可能重复:
Segmentation Fault With Char Array and Pointer in C on Linux
考虑以下代码
#include <stdio.h>
int main()
{
char a[] = "hello";
char *ptr = "world";
a[2] = 'y'; //Line1. No segmentation fault
ptr[2] = 'u'; //Line2. Causes Segmentation fault
printf("name = %s\n", a);
printf("name = %s\n", ptr);
return 0;
}
如果Line2导致分段错误,因为它试图修改字符串常量,那么为什么该规则不适用于Line1?