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

后端 未结 5 2042
野趣味
野趣味 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:26

    char *str = "string"; In such a case it is treated as a read only literal. It is similar to writing const char *str = "string". Which is to say that the value pointed to by the pointer str is a constant. Trying to edit will result in BUS ERROR.

提交回复
热议问题