Why do we use null in strok()
function?
while(h!=NULL)
{
h=strtok(NULL,delim);
if(hold!=NULL)
printf("%s",hold);
}
What does this program do when *h is pointing to a string?
Why do we use null in strok()
function?
while(h!=NULL)
{
h=strtok(NULL,delim);
if(hold!=NULL)
printf("%s",hold);
}
What does this program do when *h is pointing to a string?
str - C string to truncate. Notice that this string is modified by being broken into smaller strings (tokens). Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
delimiters - C string containing the delimiter characters. These may vary from one call to another.
More about
strtok()
see this linkstrtok() stores the pointer in static variable where did you last time left off , so on its 2nd call , when we pass the null , strtok() gets the pointer from the static variable .
If you provide the same string name , it again starts from beginning.
Moreover strtok() is destructive i.e. it make changes to the orignal string. so make sure you always have a copy of orignal one.
One more problem of using strtok() is that as it stores the address in static variables , in multithreaded programming calling strtok() more than once will cause an error. For this use strtok_r().
From the man page of strtok (I use cygwin and all posix manuals are installed)
basically strtok in subsequent calls expects NULL,with search string in the above example the first call to strtok on line in while loop which is
LINE TO BE SEPARATED
will make token point to LINE but in subsequent call it will jump the whitespace and point to TO bascially when NULL is used it token will return a pointer to location ahead of delimiter string.strtok
is part of the C library and what it does is splitting a C null-delimited string into tokens separated by any delimiter you specify.The first call to strtok must pass the C string to tokenize, and subsequent calls must specify
NULL
as the first argument, which tells the function to continue tokenizing the string you passed in first.The return value of the function returns a C string that is the current token retrieved. So first call --> first token, second call (with NULL specified) --> second token, and so on.
When there are no tokens left to retrieve,
strtok
returns NULL, meaning that the string has been fully tokenized.Here's the reference, with an example: http://www.cplusplus.com/reference/cstring/strtok/