Why do we use NULL in strtok()?
Asked Answered
F

6

33

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?

Festinate answered 4/5, 2014 at 12:45 Comment(4)
yes. i know that it parses a string using delimiters.but what does h!=NULL mean?Festinate
A null pointer is returned if there are no tokens left to retrieve..Spheno
thanks mate but why do we use it here? ----> h=strtok(NULL,delim);Festinate
@user3600999: that should be explained in your handy C reference manual. It tells strtok to continue scanning from the end of the previous token.Fevre
S
42

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/

Simba answered 4/5, 2014 at 13:2 Comment(0)
H
21

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

Hemorrhoid answered 27/1, 2018 at 11:56 Comment(0)
A
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.

Aphonia answered 26/8, 2018 at 11:10 Comment(0)
I
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

Invertebrate answered 4/5, 2014 at 13:3 Comment(0)
U
3

strtok know where it left from last execution in a static pointer. If we pass NULL as first parameter, it meanas we are going to search in previous string insted of using this function for any other strings. For example you have char *str = "1,2,3,4,5" and you called strtok(str,","). It will give you the address of first ','. When you pass NULL for first argument ( strtok(NULL,',') ) . Instead of start searching from 1, you will start searching from 2.

Unilocular answered 3/5, 2022 at 18:34 Comment(0)
N
2

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.

Nearby answered 13/9, 2019 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.