I have the following code:
#include <string.h>
int main(void) {
char *buffer = NULL, **words = NULL, *aPtr = NULL, *sPtr;
int count = 0;
buffer = strdup("The quick brown fox jumps over the lazy dog");
sPtr = buffer;
do {
aPtr = strsep(&sPtr, " ");
words[count++] = ... // missing code
} while(aPtr);
return 0;
}
I’m missing some code as you can see above… Is there any kind of strdup() that works on this situation? The strdup() function itself doesn’t seem to work… If there isn’t one, how can I make this piece of code work?
Pointer of pointer is headache for me…
As yet you have not allocated
words[0],words[1], … so usingstrdupdoesn’t help.Worse, you don’t know in advance how many words there are going to be, so it is not trivial to
mallocthe space you need.One option is to replace
wordswith a linked list, or a dynamic array.