我正在从事一个涉及使用 strtok
的项目,由于某种原因,我得到的是空值而不是实际值(应该是“二”和“三”)。这是我的代码:
int main(){
int h,z;
char text[100] = "One Two Three";
for(h = 0; h < 4; h ++){
char *first = strtok(text, " ");
printf("%s\n",first);
for(z = 0; z < 3; z++){
char *second = strtok(NULL, " ");
printf("%s\n",second);
}
}
return 0;
}
我得到的输出是:
One
Two
Three
One
(null)
(null)
One
(null)
(null)
One
(null)
(null)
我该怎么做才能获得正确的值 Two
和 Three
而不是 null?
最佳答案
您可以标记字符串,将其保存在双指针中并打印任意多次!
关于c - 使用 strtok [C] 时获取 Null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46594554/