I'm trying to create a string parser that breaks a string up into an array of strings using a delimiter. I'm using the function "strtok" which I understand how to use. What I don't understand is how to get my parsing fuction to return an array of the different words in my string. It's been quite some time since I've used C, so this is proving to be quite difficult.
char ** parseString(char * inString){
char **ptr; //What I want to return, an array of strings
int count = 0;
ptr[count] = strtok(inString, " ");
count++;
while(inString!=NULL){
ptr[count]=strtok(NULL, " ");
count++;
}
return ptr;
}
I know that this above code won't work. I vaguely remember how to use malloc, and I know that my above code will result in a seg fault since I haven't malloced, but above is essentially what I want to have happen. How do I appropriately malloc if I don't even know how many words I need to have in my array of strings?
reallocto make it bigger. If you made it too big,reallocto make it smaller at the end.inStringneed to be as its was originally?