Defined in header <string.h> | ||
---|---|---|
(1) | ||
char *strtok( char *str, const char *delim ); | (until C99) | |
char *strtok( char *restrict str, const char *restrict delim ); | (since C99) | |
char *strtok_s(char *restrict str, rsize_t *restrict strmax, const char *restrict delim, char **restrict ptr); | (2) | (since C11) |
str
. The separator characters are identified by null-terminated byte string pointed to by delim
.str != NULL
, the call is treated as the first call to strtok
for this particular string. The function searches for the first character which is not contained in delim
. str
at all, and the function returns a null pointer. delim
. str
has only one token, and future calls to strtok
will return a null pointer '\0'
and the pointer to the following character is stored in a static location for subsequent invocations. str == NULL
, the call is treated as a subsequent calls to strtok
: the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as str
. str
or delim
is not a pointer to a null-terminated byte string.str
into *strmax
and writes the tokenizer's internal state to *ptr
. Repeat calls (with null str
) must pass strmax
and ptr
with the values stored by the previous call. Also, the following errors are detected at runtime and call the currently installed constraint handler function, without storing anything in the object pointed to by ptr
strmax
, delim
, or ptr
is a null pointer str
), *ptr
is a null pointer *strmax
is zero or greater than RSIZE_MAX
*strmax
)) without encountering the null terminatorstr
points to a character array which lacks the null character and strmax
points to a value which is greater than the size of that character array. As all bounds-checked functions, strtok_s
is only guaranteed to be available if __STDC_LIB_EXT1__
is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__
to the integer constant 1
before including string.h
.str | - | pointer to the null-terminated byte string to tokenize |
delim | - | pointer to the null-terminated byte string identifying delimiters |
strmax | - | pointer to an object which initially holds the size of str : strtok_s stores the number of characters that remain to be examined |
ptr | - | pointer to an object of type char* , which is used by strtok_s to store its internal state |
Returns pointer to the beginning of the next token or NULL
if there are no more tokens.
This function is destructive: it writes the '\0'
characters in the elements of the string str
. In particular, a string literal cannot be used as the first argument of strtok
.
Each call to strtok
modifies a static variable: is not thread safe.
Unlike most other tokenizers, the delimiters in strtok
can be different for each subsequent token, and can even depend on the contents of the previous tokens.
The strtok_s
function differs from the POSIX strtok_r function by guarding against storing outside of the string being tokenized, and by checking runtime constraints.
#define __STDC_WANT_LIB_EXT1__ 1 #include <string.h> #include <stdio.h> int main(void) { char input[] = "A bird came down the walk"; printf("Parsing the input string '%s'\n", input); char *token = strtok(input, " "); while(token) { puts(token); token = strtok(NULL, " "); } printf("Contents of the input string now: '"); for(size_t n = 0; n < sizeof input; ++n) input[n] ? printf("%c", input[n]) : printf("\\0"); puts("'"); #ifdef __STDC_LIB_EXT1__ char str[] = "A bird came down the walk"; rsize_t strmax = sizeof str; const char *delim = " "; char *next_token; printf("Parsing the input string '%s'\n", str); token = strtok_s(str, &strmax, delim, &next_token); while(token) { puts(token); token = strtok_s(NULL, &strmax, delim, &next_token); } printf("Contents of the input string now: '"); for(size_t n = 0; n < sizeof str; ++n) str[n] ? printf("%c", str[n]) : printf("\\0"); puts("'"); #endif }
Possible output:
Parsing the input string 'A bird came down the walk' A bird came down the walk Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0' Parsing the input string 'A bird came down the walk' A bird came down the walk Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'
finds the first location of any character in one string, in another string (function) |
|
returns the length of the maximum initial segment that consists of only the characters not found in another byte string (function) |
|
returns the length of the maximum initial segment that consists of only the characters found in another byte string (function) |
|
(C95)(C11) | finds the next token in a wide string (function) |
C++ documentation for strtok |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/string/byte/strtok