-
strtok()
I was reading The C Programming Language by Kernighan and Ritchie. I noticed a reference to a function in the standard library header string.h that I might be able to find a lot of uses for in my programs, strtok().
Could someone show me a bit of code describing how this function works, as my book doesn't say how to use it, only what the function declaration is and a description of what it does.
-CeeCee
-
It's amazing what a board search for the word strtok can produce...
Quzah.
-
Okay, point made. But I still have a few questions
Code:
#include <stdio.h>
#include <string.h>
int main ( ) {
char test_msg[]="This is a test message";
char *p;
char *delim = " ";
char *words[20];
int i = 0, j;
p = strtok(test_msg,delim);
while ( p ) {
words[i] = p;
p = strtok(NULL,delim);
i++;
}
printf ( "%d\n", i );
for ( j = 0 ; j < i ; j++ ){
printf("'%s'\n", words[j]);
}
}
p = strtok(NULL,delim);
What does this do and why? I know that it assigns a token to the char pointer p, and that spaces don't count as tokens, but what is NULL there for?
-
I think the first call to strtok returns a pointer to the first word ("This" in this case). The subsequent calls to NULL force the pointer up the string to the next token without reassigning it to anywhere else.
I think anyway.....:confused:
-
> I think the first call to strtok returns a pointer to the first word
> ("This" in this case). The subsequent calls to NULL force the
> pointer up the string to the next token without reassigning it to
> anywhere else.
Yup. Good explaination. For another description, try here.
I like the comment:
"Never use this function."
Quzah.
-
Never use this function? I find that a bit funny because if it weren't meant to be used, why have it? Besides, if I wanted to extract tokens from a string then the last thing I would want is to leave the string unchanged. Even if for some strange reason I did I could copy the string and change the copy.
Is that just the opinion of the person who wrote that?
-
All you have to remember about strtok is that if you need to keep the original data then tokenise a copy. The modification of the original data is the only downside to strtok.