I am using C++ to deal with string operation for long time but for some reason I have to write a code in C. I know that in C, I have to allocate the memory first before string operation. I am going to convert the array of integer to a big string, e.g.
int data[]={1, 102, 3024, 2, 3, 50234, 23} => "1,102,3024,2,3,50234,23"
I am using sprintf to convert each number in the array to string and use strcat to concatenate each substring
char *s, *output_string;
int i, N;
// N is the size of the array and given as parameter of a function
for (i=0; i<N; i++)
{
sprintf(s, "%d", data[i]);
strcat(output_string, s);
}
But it doesn't work. I wonder to use those functions sprintf and strcat, do I have to allocate the memory first? But the number of digits of the data[i] is varied so how can I tell how much memory I should assigned in advance? Also, how can I tell how much memory in total I have to allocate for output_string in advance? Thanks