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

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

    You are trying to modify a string constant. Use this instead:

    char a[] = "abc";
    a[0] = 'c';
    

提交回复
热议问题