Jonathan Leffler's answer outlines a number of things you will need to sort out before your code will work. If you haven't already, you should give him an upvote :)
In your current code, the cast to (char*)
in your code tells the compiler to treat the memory where asc
is stored as a char*
, but it won't do the conversion to string for you.
In order to convert a double
to a string, have a look at sprintf()
. I would replace the strcat lines with this:
sprintf(strAll,"%s%s %f",temp,str,asc);
Note that you would first need to make sure that strAll
is big enough to contain the resulting string. This could be a little bit tricky for a double
. However, I don't really think you have a double
- clock_t
is probably a long
, which means you could do this:
long asc = (long) curtime;
...
strAll = malloc(strlen(temp) + strlen(str) + 1 + (asc/10 +1) + 1);
sprintf(strAll,"%s%s %ld",temp,str,asc);
Warning: asc/10 +1
only tells you how many characters you need to allocate for a non-negative number. Since clock is documented to return negative in error cases, you should first check for that.
Note that I also removed the cast to char*
since it can cause problems in C (thanks to an anonymous commenter).
Also: Currently the result of malloc isn't checked for errors - on error, it will return NULL
, which will break attempts to use strAll
. Consequently, it's a good idea to make sure it isn't NULL
before trying to use it.