Why do we use NULL in strtok()?

39,872

Solution 1

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/

Solution 2

strtok() 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().

Solution 3

char * strtok ( char * str, const char * delimiters );

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 link

Solution 4

From the man page of strtok (I use cygwin and all posix manuals are installed)

 Searching for Word Separators
       The following example searches for tokens separated by <space> characters.

           #include <string.h>
           ...
           char *token;
           char line[] = "LINE TO BE SEPARATED";
           char *search = " ";

           /* Token will point to "LINE". */
           token = strtok(line, search);

           /* Token will point to "TO". */
           token = strtok(NULL, search);

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.

Solution 5

A sequence of calls of char * strtok ( char * str, const char * delimiters ) splits string into tokens. The first call to strtok() takes the string and delimiters as input and returns the first token of the string.

To find the remaining tokens of the string subsequent call is required and subsequent call is specified by passing the NULL as input to strtok( NULL , const char *delimiters ).

It returns NULL when no further token is found.

Note: The string "delimiters" may be different on each call.

Share:
39,872

Related videos on Youtube

user3600999
Author by

user3600999

Updated on May 04, 2022

Comments

  • user3600999
    user3600999 about 2 years

    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?

    • Joe
      Joe about 10 years
      Did you do any research or reading on what strtok actually does?
    • user3600999
      user3600999 about 10 years
      yes. i know that it parses a string using delimiters.but what does h!=NULL mean?
    • Joachim Isaksson
      Joachim Isaksson about 10 years
    • user3600999
      user3600999 about 10 years
      thanks mate but why do we use it here? ----> h=strtok(NULL,delim);
    • John Bode
      John Bode about 10 years
      @user3600999: that should be explained in your handy C reference manual. It tells strtok to continue scanning from the end of the previous token.