以下代码在编译期间产生分段错误:
(gdb) 运行
启动程序:/home/anna/Desktop/a.out
程序收到信号 SIGSEGV,分段错误。
/lib/i386-linux-gnu/libc.so.6 中的 strtok() 中的 0xb7e97845
#include <string.h>
#include <stdio.h>
main () {
char * sentence = "This is a sentence.";
char * words[200] ;
words[0] = strtok(sentence," ");
}
更改第 5 行后,没有抛出错误。
#include <string.h>
#include <stdio.h>
main () {
char sentence[] = "This is a sentence.";
char * words[200] ;
words[0] = strtok(sentence," ");
}
为什么会这样?