1strtok(3) Library Functions Manual strtok(3)
2
3
4
6 strtok, strtok_r - extract tokens from strings
7
9 Standard C library (libc, -lc)
10
12 #include <string.h>
13
14 char *strtok(char *restrict str, const char *restrict delim);
15 char *strtok_r(char *restrict str, const char *restrict delim,
16 char **restrict saveptr);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 strtok_r():
21 _POSIX_C_SOURCE
22 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
23
25 The strtok() function breaks a string into a sequence of zero or more
26 nonempty tokens. On the first call to strtok(), the string to be
27 parsed should be specified in str. In each subsequent call that should
28 parse the same string, str must be NULL.
29
30 The delim argument specifies a set of bytes that delimit the tokens in
31 the parsed string. The caller may specify different strings in delim
32 in successive calls that parse the same string.
33
34 Each call to strtok() returns a pointer to a null-terminated string
35 containing the next token. This string does not include the delimiting
36 byte. If no more tokens are found, strtok() returns NULL.
37
38 A sequence of calls to strtok() that operate on the same string main‐
39 tains a pointer that determines the point from which to start searching
40 for the next token. The first call to strtok() sets this pointer to
41 point to the first byte of the string. The start of the next token is
42 determined by scanning forward for the next nondelimiter byte in str.
43 If such a byte is found, it is taken as the start of the next token.
44 If no such byte is found, then there are no more tokens, and strtok()
45 returns NULL. (A string that is empty or that contains only delimiters
46 will thus cause strtok() to return NULL on the first call.)
47
48 The end of each token is found by scanning forward until either the
49 next delimiter byte is found or until the terminating null byte ('\0')
50 is encountered. If a delimiter byte is found, it is overwritten with a
51 null byte to terminate the current token, and strtok() saves a pointer
52 to the following byte; that pointer will be used as the starting point
53 when searching for the next token. In this case, strtok() returns a
54 pointer to the start of the found token.
55
56 From the above description, it follows that a sequence of two or more
57 contiguous delimiter bytes in the parsed string is considered to be a
58 single delimiter, and that delimiter bytes at the start or end of the
59 string are ignored. Put another way: the tokens returned by strtok()
60 are always nonempty strings. Thus, for example, given the string
61 "aaa;;bbb,", successive calls to strtok() that specify the delimiter
62 string ";," would return the strings "aaa" and "bbb", and then a null
63 pointer.
64
65 The strtok_r() function is a reentrant version of strtok(). The
66 saveptr argument is a pointer to a char * variable that is used inter‐
67 nally by strtok_r()