我在学习strtok函数的代码从BSD的libc中,当我在我的机器上运行它,程序接收到的信号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;
}
Run Code Online (Sandbox Code Playgroud)
s[-1]
Run Code Online (Sandbox Code Playgroud)
扩大到:
*( s - 1 )
Run Code Online (Sandbox Code Playgroud)
因此,如果结果指向有效内存,则定义代码.