Okay, I seem to have got a working method, although it does seem a little long-winded for something as simple as string concatenation:
Code:
int c,r;
char *str;
char tmp[10];
//The initial size of the string buffer
int strSize = (100 * sizeof(char));
//Allocate memory for the string buffer
str = (char *) malloc(strSize);
strcpy(str, "");
//Add one element of the array at a time
for (r=0; r<array->rows; r++) {
for (c=0; c<array->columns; c++) {
//Allocate more memory to the string buffer if required
if (strlen(str) > (strSize - 25)) {
strSize += (100 * sizeof(char));
str = realloc(str, strSize);
}
t = array2d_get(array, c, r);
sprintf(tmp, "%d ", t);
str = strcat(str, tmp);
}
//Add a newline character for each row
str = strcat(str, "\n");
}
return str;
This seems to work well enough but I doubt it's the best way; can anyone give me any useful tips (Aside from optimising it)? I'd imagine someone with more experience (i.e. more than a week ) could come up with a better solution...