Why Segmentation fault in following code?

后端 未结 5 1221
忘了有多久
忘了有多久 2021-01-21 14:18

I read this on wikipedia

    int main(void)
 {

    char *s = \"hello world\";
    *s = \'H\';

 }

When the program containing this code is com

5条回答
  •  隐瞒了意图╮
    2021-01-21 14:36

    There is a big difference between:

    char * s = "Hello world";
    

    and

    char s[] = "Hello world";
    

    In the first case, s is a pointer to something that you can't change. It's stored in read-only memory (typically, in the code section of your application).

    In the latter case, you allocate an array in read-write memory (typically plain RAM), that you can modify.

提交回复
热议问题