Hi, everyone. I have a function that optionally produces up to 3 strings. Each string has a maximum length, which is unique to each string.
I can do it like this:
Code:
void producestrings (char (*sz1)[a], char (*sz2)[b], char (*sz3)[c]);
char sz1[a];
char sz2[b];
char sz3[c];
producestrings (&sz1, &sz2, &sz3);
in which case the function risks wasting some memory if the strings are not as long as they could be. Or I can do this:
Code:
bool producestrings ( char **sz1, char **sz2, char **sz3);
char *sz1;
char *sz2;
char *sz3;
producestrings(&sz1, &sz2, &sz3);
in which case the function allocates memory with malloc(), which must be free()d by the calling function. This means manual memory handling and the risk of forgetting to free() the memory.
Which is better?
TIA,
Richard