strtok does not redistribute anything. It only makes clippings and pointers of what you gave him.
Your array stores pointers that strtok provides but do not copy the contents.
So, if you free your tempString variable, you will get free data that was indicated by strtok return values. You must save it and release it only at the end.
Or you can make strdup each strtok return to store it in your array to make a real copy of each token, but in this case you will need to free each token at the end.
The second solution will look like this:
void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) {
And after using this array, you will have to delete it using the following function:
void deleteTokens(char **arrToStoreTokens, int arraySize) { int x; for (x = 0; x < arraySize; ++x) { free(arrToStoreTokens[x]); } }
source share