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?
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/
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().
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.
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
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
A null pointer is returned if there are no tokens left to retrieve.
. – Spheno