Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtok() - Why you have to pass the NULL pointer in order to get the next token in the string?

Tags:

This is the explanation of strtok().

#include <string.h> char* strtok( char* s1,                const char* s2 );* 

The first call to strtok() returns a pointer to the first token in the string pointed to by s1. Subsequent calls to strtok() must pass a NULL pointer as the first argument, in order to get the next token in the string.

But I don't know, why you have to pass the NULL pointer in order to get the next token in the string. I searched about 15 minutes, but didn't find an explanation in the internet.

like image 603
phez1 Avatar asked Apr 14 '15 22:04

phez1


People also ask

Why do you pass NULL to strtok?

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.

How do you get next token in strtok?

The strtok() function gets the next token from string s1, where tokens are strings separated by characters from s2. To get the first token from s1, strtok() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok() with a null pointer for the first parameter.

What is the purpose of strtok () function?

The strtok() function reads string1 as a series of zero or more tokens, and string2 as the set of characters serving as delimiters of the tokens in string1. The tokens in string1 can be separated by one or more of the delimiters from string2.

What does strtok () do in C?

The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.


2 Answers

strtok() keeps some data inside of itself by using static variables. This way, strtok() can continue searching from the point it left off at during the previous call. To signal strtok() that you want to keep searching the same string, you pass a NULL pointer as its first argument. strtok() checks whether the first argument is NULL and if it is, it uses its currently stored data. If the first parameter is not null, it is treated as a new search and all internal data is reset.

Maybe the best thing you can do is to search for an actual implementation of the strtok() function. I've found one small enough to post it here, so you get an idea of how to handle this NULL parameter:

/* Copyright (c) Microsoft Corporation. All rights reserved. */  #include <string.h>  /* ISO/IEC 9899 7.11.5.8 strtok. DEPRECATED.  * Split string into tokens, and return one at a time while retaining state  * internally.  *  * WARNING: Only one set of state is held and this means that the  * WARNING: function is not thread-safe nor safe for multiple uses within  * WARNING: one thread.  *  * NOTE: No library may call this function.  */  char * __cdecl strtok(char *s1, const char *delimit) {     static char *lastToken = NULL; /* UNSAFE SHARED STATE! */     char *tmp;      /* Skip leading delimiters if new string. */     if ( s1 == NULL ) {         s1 = lastToken;         if (s1 == NULL)         /* End of story? */             return NULL;     } else {         s1 += strspn(s1, delimit);     }      /* Find end of segment */     tmp = strpbrk(s1, delimit);     if (tmp) {         /* Found another delimiter, split string and save state. */         *tmp = '\0';         lastToken = tmp + 1;     } else {         /* Last segment, remember that. */         lastToken = NULL;     }      return s1; } 
like image 178
mcleod_ideafix Avatar answered Sep 19 '22 15:09

mcleod_ideafix


If you pass a non-NULL value, you are asking it to start tokenizing a different string.

If you pass a NULL value, you are asking to continue tokenizing the same thing as before.

like image 31
StilesCrisis Avatar answered Sep 17 '22 15:09

StilesCrisis