Why Segmentation fault in following code?

后端 未结 5 1214
忘了有多久
忘了有多久 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:37

    first have a good understanding of pointers, I will give u a short demo:

    First let us analyze your code line by line. Lets start from main onwards

    char *s = "Some_string";

    first of all, you are declaring a pointer to a char variable, now *s is a address in memory, and C will kick you if you try to change its memory value, thats illegal, so u better declare a character array, then assign s to its address, then change s.

    Hope you get, it. For further reference and detailed understanding, refer KN King: C programming A Modern Approach

提交回复
热议问题