strtok_r() — Tokenize String (Restartable)
Format
#include <string.h>
char *strtok_r(char *string, const char *seps,
char **lasts);
Language Level
XPG4
Threadsafe
Yes
Locale Sensitive
The behavior of this function might be affected by the LC_CTYPE category of the current locale. For more information, see Understanding CCSIDs and Locales.
Description
This function is the restartable
version of strtok()
.
The strtok_r()
function
reads string as a series of zero or more
tokens, and seps as the set of characters
serving as delimiters of the tokens in string.
The tokens in string can be separated by
one or more of the delimiters from seps.
The arguments lasts points to a user-provided
pointer, which points to stored information necessary for the strtok_r()
function
to continue scanning the same string.
In the first call to the strtok_r()
function
for a given null-ended string, it searches
for the first token in string, skipping
over leading delimiters. It returns a pointer to the first character
of the first token, writes a null character into string immediately
following the returned token, and updates the pointer to which lasts points.
To read the next token from string,
call the strtok_r()
function
with a NULL string argument. This causes
the strtok_r()
function
to search for the next token in the previous token string. Each delimiter
in the original string is replaced by a
null character, and the pointer to which lasts points
is updated. The set of delimiters in seps can
vary from call to call, but lasts must remain
unchanged from the previous call. When no tokens remain in string,
a NULL pointer is returned.
Return Value
The first time the strtok_r()
function
is called, it returns a pointer to the first token in string.
In later calls with the same token string, the strtok_r()
function
returns a pointer to the next token in the string. A NULL pointer
is returned when there are no more tokens. All tokens are null-ended.