可能重複:
Why do I get a segmentation fault when writing to a string?
What is the difference between char a[] = 「string」; and char *p = 「string」;更改字符邊走邊串崩潰程序
任何人都可以指出在下面的程序問題:
#include <stdio.h>
int main()
{
char *c = "Hello World!! !!";
char c2 = 'X';
while(c)
{
if(*c == ' ')
{
printf("%s",c);
*c = c2;
}
c++;
}
return 0;
}
它崩潰在*c = c2;
機智h以下錯誤:
Thread [1] (Suspended : Signal : EXC_BAD_ACCESS:Could not access memory)
main() at mainclass.cpp:64 0x100000d74
我使用GCC作爲MAC OSX和Eclipse上的編譯器作爲IDE。
這只是一個SegFault。 –