简体   繁体   English

strtok_s如何标记char *?

[英]How does strtok_s tokenize a char*?

It seems to have modified the original string that I wanted to split specifying tokens. 它似乎修改了我想拆分指定标记的原始字符串。

how does it return back a substring if it can't copy from memory? 如果无法从内存中复制,它如何返回子串?

I'm also looking for an alternative that accepts a const char* Or doesn't modify the orignal string. 我也在寻找一个接受const char*的替代方案const char*或者不修改orignal字符串。

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**) .? 或者只是const_cast字符串以删除const属性并让它由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_cast d constant string. strtok_s修改传递的字符串,所以不,你不能在传递const_cast d常量字符串。 What it does is if it finds a delimiter, it replaces that delimiter with a NUL terminator to separate the string into two pieces. 它的作用是如果它找到一个分隔符,它用一个NUL终结符替换该分隔符,将该字符串分成两部分。 It then returns a pointer to the first part, which is now the desired null-terminated token. 然后它返回指向第一部分的指针,该指针现在是所需的以空值终止的标记。

如果您不想修改原始字符串,可以strtok_s其复制,然后strtok_s其复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM