Quoting C11
, chapter §7.24.5.8, P4 (emphasis mine)
The strtok
function then searches from there for a character that is contained in the
current separator string. If no such character is found, the current token extends to the
end of the string pointed to by s1
, and subsequent searches for a token will return a null
pointer. If such a character is found, it is overwritten by a null character, which
terminates the current token. The strtok
function saves a pointer to the following
character, from which the next search for a token will start.
and P5,
Each subsequent call, with a null pointer as the value of the first argument, starts
searching from the saved pointer [...]
So, this is a feature which enables strtok()
to continue parsing the leftover of the initially supplied string.
To put it in other words, an input string, containing multiple instances of the delimiter can be parsed using a simple loop using this feature. For example, if a "sentence" is to be tokenized based on the "whitespace"s to separate out each "word" , then, after the first call, a simple loop, calling strtok()
with NULL
as first argument will make use of internal save pointer and require less explicit effort to manage the remainder of the actual sentence.
Without this feature, manual effort would have been required to keep a tab of the progress made in search of the delimiter.