Do it this way:
char * recv_str = malloc(PRECISION * sizeof(*recv_str)); /* Make sure PRECISION is large enough!!! */
...
char str_exp[32] = ""; /* Make it's large enough to hold the decimal representaion of a long. */
char * deli = "@";
if (NULL == recv_str)
{
perror("malloc() failed for recv_str");
assert(1);
}
for(i=1; i<= size - 1 ; i++)
{
strcpy(recv_str, ""); /* Reset the "string" to "empty" before each iteration! */
...
sprintf(str_exp, "%ld", exp);
assert(sizeof(recv_str) <= (strlen(recv_str) + strlen(deli)));
strcat(recv_str, deli);
assert(sizeof(recv_str) <= (strlen(recv_str) + strlen(str_exp)));
strcat(recv_str, str_exp);
...
}
free(recv_str);
Update:
As it showed that initialsing recv_str
to 0
-length by doing
strcpy(recv_str, "");
did not solve the problem, as it most probably only sets the "string"'s 1st byte to NUL
/'\0'
, the more radical approach needed to be taken to clear out the whole "string" with 0
s by doing:
memset(recv_str, 0, PRECISION * sizeof(*recv_str));