strtok函数

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。
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值