开发者

How does strtok_s tokenize a char*?

开发者 https://www.devze.com 2023-01-27 17:12 出处:网络
It seems to have modified the original string that I wanted to split specifying tokens. how does it return back a substring if i开发者_如何转开发t can\'t copy from memory?

It seems to have modified the original string that I wanted to split specifying tokens.

how does it return back a substring if i开发者_如何转开发t can't copy from memory?

I'm also looking for an alternative that accepts a const char* Or doesn't modify the orignal string.

Or is it safe to just const_cast the string to remove the const property and let it be handled by strtok_s(char*, const char*, char**).?


strtok_s et al do modify the strings passed in, so no, you can't pass in a const_castd constant string. What it does is if it finds a delimiter, it replaces that delimiter with a NUL terminator to separate the string into two pieces. It then returns a pointer to the first part, which is now the desired null-terminated token.


If you don't want to modify the original string, you can always copy it first and then strtok_s it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号