Reputation: 180
I am learning string manipulation with C Standard Functions. When I am learning this stuff, I am facing with strtok function and the following code.
#include <string.h>
#include <stdio.h>
int main()
{
char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = "-";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );
token = strtok(NULL, s);
}
return(0);
}
I don't understand why in while loop, strtok used with null? Why null used here? Because in strtok function definition comes something like (this function breaks first paramter string into a series of tokens using the second paramter of itself.)
Upvotes: 1
Views: 667
Reputation: 53006
Because it uses an internal static
pointer to the string you are working with, so if you want it to operate on the same string, you just need to call it with NULL
as the first argument and let it use it's internal pointer. If you call it with non-null first argument then it will overwrite the pointer with the new pointer.
This means in turn, that strtok()
is not reentrant. So you normally just use it in simple situations, more complex situations where reentrance is important (like multithreaded programs, or working on multiple strings) require different approaches.
One way is on POSIX systems where you can use strtok_r()
which takes one extra argument to use as it's "internal" pointer.
Check this manual to learn about it more.
Upvotes: 7
Reputation: 515
The first call you use a char array which has the elements you want parsed.
The second time you call it you pass it NULL
as the first parameter to tell function to resume from the last spot in the string. Once the first call is made your char
array receives the parsed string. If you don't put NULL
you would lose your place and effectively the last part of your string.
char * c_Ptr = NULL; //temp hold indivisual sections
char c_Ptr1[1000] = {NULL};
fgets(c_Ptr1, 1000, f_ptr); //Grabs a line from an open file
strtok(c_Ptr1, ","); //first one starts at the beginning
c_Ptr = strtok(NULL, ",");
Upvotes: 2
Reputation: 137322
strtok
uses an internal (static) state to tokenize a string. When called with NULL, it goes to the next token in the string that was passed in the first call.
It is worth mentioning, that this property (internal state) makes it unsafe to use in multi-threaded environment. A safer version is strtok_r
, which return the state as an output parameter.
Upvotes: 4