Understanding strtok from string.h library
From my book I did the following example
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "This is a sentence with 7 tokens";
char *tokenPtr;
printf( "%s\n%s\n\n%s\n",
"The string to be tokenized is:", string,
"The tokens are:" );
tokenPtr = strtok( string, " " );
while ( tokenPtr != NULL ) {
printf( "%s\n", tokenPtr );
tokenPtr = strtok( NULL, " " );
}
return 0;
}
What I'm noticing is that the token isn't being saved to a new array. A pointer is just pointed at the existing array. If that is the case how come printf doesn't print everything after tokenPtr? You would expect %s to print the whole array no?