#include <string.h> char *strtok(char *_Nullable restrict str, const char *restrict delim);
delim 引数には、解析対象の文字列をトークンに区切るのに使用する バイト集合を指定する。同じ文字列を解析する一連の呼び出しにおいて、 delim に違う文字列を指定してもよい。
strtok() のそれぞれの呼び出しでは、次のトークンを格納したヌル終端 された文字列へのポインターが返される。この文字列には区切りバイトは含まれ ない。これ以上トークンが見つからなかった場合には、NULL が返される。
同じ文字列に対して操作を行う strtok() を連続して呼び出す場合、 次のトークンを探し始める位置を決めるためのポインターが保持される。 最初の strtok の呼び出しでは、 このポインターは対象の文字列の最初のバイトにセットされる。 次のトークンの先頭は、 str 内で次の区切りバイト以外のバイトを前方に検索して決定される。 区切りバイト以外のバイトが見つからなかった場合は、 トークンはこれ以上なく、 strtok() は NULL を返す (したがって、 空の文字列や区切りバイトだけを含む文字列の場合には、 最初の strtok() の呼び出しで NULL が返ることになる)。
The end of each token is found by scanning forward until either the next delimiter byte is found or until the terminating null byte ('\0') is encountered. If a delimiter byte is found, it is overwritten with a null byte to terminate the current token, and strtok() saves a pointer to the following byte; that pointer will be used as the starting point when searching for the next token. In this case, strtok() returns a pointer to the start of the found token.
From the above description, it follows that a sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter, and that delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings. Thus, for example, given the string "aaa;;bbb,", successive calls to strtok() that specify the delimiter string ";," would return the strings "aaa" and "bbb", and then a null pointer.
| インターフェース | 属性 | 値 |
| strtok() | Thread safety | MT-Unsafe race:strtok |