我正在研究来自BSD libc的函数strtok的代码,当我在我的机器上运行时,程序接收到了信号
以下是我的代码:
SIGSEGV
,并显示错误原因为s [-1] = 0
。这里是该代码的链接。
s[-1] = 0
是否正确?以下是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include "strtok.c"
int main(int argc, char* argv[]) {
char* str = "xxxx xxxyy fdffd";
const char* s = " ";
char* token = strtok(str, s);
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, s);
}
return 0;
}
s[-1] = 0
是正确的。可能调用它的代码有问题。 - Dietrich Eppstrtok()
传递一个不可修改的字符串(即字符串字面值)。strtok()
修改了传入的字符串,因此字符串字面值是不行的。 - Michael Burr