只是爲了測試我已經創建了下面的代碼:段故障
#include<stdio.h>
int main(){
char *p = "Hello world";
*(p+1) = 'l';
printf("%s", p);
return 0;
}
但是,當我在ubuntu下10.04跑這在我的「GCC」編譯我:
Segmentation fault
所以誰能解釋爲什麼會發生這種情況。
#include<stdio.h>
#include<stdlib.h>
int main(){
char *p = malloc(sizeof(char)*100);
p = "Hello world";
*(p+1) = 'l';
printf("%s", p);
free(p);
return 0;
}
這也導致段錯誤 由於提前
可能重複接收到分段錯誤?](http://stackoverflow.com/questions/164194/why-does-simple-c-code-receive-segmentation-fault) – Joe 2010-12-11 12:38:48
...以及過去幾年中大約二十個其他SO問題。 – Joe 2010-12-11 12:39:06