【发布时间】:2021-10-20 04:53:13
【问题描述】:
下面的代码给出了意外的行为。
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "hello $";
char *t;
t = strtok(s, "$$");
printf("%s", t);
return 0;
}
为什么输出hello 而不是hello $?
【问题讨论】:
-
该函数使用第二个参数中的 set 个字符进行标记,它将仅使用第二个参数字符串中的一个字符。
-
第二个参数是一个包含多个独立单字符分隔符的字符串,不是子字符串。
-
那我应该如何用字符串分割呢?
-
“strtok 奇怪的行为”是不言而喻的。它的怪异是设计使然。
标签: c string tokenize c-strings strtok