|
From: Tuomo L. <dj...@mb...> - 2005-05-17 10:46:57
|
Liam Whalen wrote:
> if I malloc data for dataToken before calling parseData the function works as expected
> and I can use dataToken after the call to parseData. However if I pass in a simple char*
> and malloc memory for dataToken inside parseData when parseData returns dataToken
> contains seemingly random data.
As others have already stated, this is because the change is not
passed back to the calling function. You are only changing the variable
inside the local scope. The solution is to use pointer to the
pointer (char **dataToken) as parameter.
> tokenSize = TOKEN_STRING_SIZE;
...
> dataToken = (char*) malloc(tokenSize);
I hope you are aware of the fact that you generally need to
allocate (string_length + 1) because the string is terminated
with zero character ('\0', ascii control character NUL).
AFAIK all libc string routines depend on this.
--
Tuomo
... Adding 100 to 486 made 585.999874653, so they named it "Pentium"
|