在下面的代碼中,爲什麼不能解除引用:*arr = 'e'
。不應該輸出字符'e'
?無法解除引用
int main()
{
char *arr = "abt";
arr++;
* arr='e'; // This is where I guess the problem is occurring.
cout << arr[0];
system("pause");
}
我收到以下錯誤:
Unhandled exception at 0x00a91da1 in Arrays.exe: 0xC0000005: Access violation writing location 0x00a97839.
如果 「ABT」 是常量字符串文字,那麼爲什麼下面的代碼修改相同的常量字面量:int main(void) { \t char arr [] =「abt」; \t arr [0] ='e'; \t cout << arr [0]; \t system(「pause」); } –
可能的重複[爲什麼這C代碼導致分段錯誤?](http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault) – AnT