A
Anthony
Hello,
I am writing a function that populates an array of pointers to
strings. Both the number of strings in the array, and the lengths of
the strings, are dynamic; in particular, the number of strings won't
be known until just before the function returns.
The problem is that in the calling function, I need to know both the
address of the array, and the number of strings. My first thought was
to pass a pointer to the array into the function, and return a size_t
which is the number of strings. That way, I know the address of the
array to begin with, and I get the number of strings when the function
returns. But since I realloc() within the function, I change the
address of the array, so the address in the calling function is no
longer correct.
At this point, I think that I'll need to create a structure to hold
both the array and the number of strings, and return a pointer to an
instance of the struct.
Is my thinking correct on this? Is there a different/better approach?
Thanks,
Anthony
http://nodivisions.com/
I am writing a function that populates an array of pointers to
strings. Both the number of strings in the array, and the lengths of
the strings, are dynamic; in particular, the number of strings won't
be known until just before the function returns.
The problem is that in the calling function, I need to know both the
address of the array, and the number of strings. My first thought was
to pass a pointer to the array into the function, and return a size_t
which is the number of strings. That way, I know the address of the
array to begin with, and I get the number of strings when the function
returns. But since I realloc() within the function, I change the
address of the array, so the address in the calling function is no
longer correct.
At this point, I think that I'll need to create a structure to hold
both the array and the number of strings, and return a pointer to an
instance of the struct.
Is my thinking correct on this? Is there a different/better approach?
Thanks,
Anthony
http://nodivisions.com/