strtok用了static变量来保存原来的地址,所以后面可以用NULL来代替S。
也因为如此,strtok属于非线程安全的函数。尽量不要去用它.
MSDN的原文
Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible
delimiters of the token to be found in strToken on the current call. wcstok is the wide-character version
of strtok. The arguments and return value of wcstok are wide-character strings. These two functions
behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE Defined
_tcstok wcstok
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in
strToken, terminating the token with a null character. More tokens can be broken out of the remainder
of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character
after the token returned by that call. To read the next token from strToken, call strtok with a NULL value
for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the
modified strToken. The strDelimit argument can take any value from one call to the next so that the set of
delimiters may vary.
Warning Each of these functions uses a static variable for parsing the string into tokens. If multiple or
simultaneous calls are made to the same function, a high potential for data corruption and inaccurate
results exists. Therefore, do not attempt to call the same function simultaneously for different strings
and be aware of calling one of these functions from within a loop where another routine may be called
that uses the same function. However, calling this function simultaneously from multiple threads does
not have undesirable effects.
strtok_r()函数是strtok()函数的可重入版本,它需要一个指向用户开辟的空间的char*型指针,用以取代它自己的
静态缓冲区。这个指针,即ptrptr参数,在对同一个字符串处理的时候,必须相同。
尽量不要使用这些函数,如果你用,那么记住这些:
1 这些函数会改动原始的参数。
2 这些函数不能用在常量字符串上。
3 定界符的字符将会丢失。
4 strtok()函数在分析的时候使用一个静态的缓冲区,所以它不是线程安全的。如果这个困扰你,那么使
用strtok_r()函数。
返回值:
strtok()函数返回一个指向下一个单词的指针,或者在没有单词的时候返回NULL。
strtok函数
最新推荐文章于 2023-09-13 06:48:53 发布