字符数组初始化产生分段错误

ann*_*nna 1 c arrays segmentation-fault

以下代码在编译期间产生分段错误:

(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," ");
}
Run Code Online (Sandbox Code Playgroud)

更改第5行后,不会抛出任何错误.

#include <string.h>
#include <stdio.h>

main () {
char  sentence[] = "This is a sentence.";
char * words[200] ;
words[0] = strtok(sentence," ");
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

Alo*_*ave 6

char * sentence = "This is a sentence.";
Run Code Online (Sandbox Code Playgroud)

sentence是指向字符串文字"这是一个句子"的指针.存储在只读存储器中,您不应该修改它.
以任何方式修改字符串文字会导致未定义的行为,在您的情况下,它会出现分段错误.

好读:
char a [] =?string有什么区别?和char*p =?string?;?