Why can't I edit a char in a char*?

后端 未结 5 2035
野趣味
野趣味 2020-12-19 06:57

Below is an exceedingly simple example. It compiles fine using gcc on Mac OS X (Snow Leopard). At runtime it outputs Bus error: 10. What\'s happening here?



        
5条回答
  •  天涯浪人
    2020-12-19 07:14

    This

    char* a = "abc";
    

    relies on a dangerous implicit conversion from const char[] (the type of a string literal) to char*. (In C++ this conversion has been deprecated for more than a decade. I don't know about C, though.)

    A string literal must not be altered.

提交回复
热议问题