getting segmentation fault in a small c program

后端 未结 3 1579
情书的邮戳
情书的邮戳 2020-12-12 04:08

i wrote a small prog :

  1 #include  
  2 main(){  
  3         char* str = \"string\";  
  4         *str = \'k\';  
  5         printf(\"str         


        
3条回答
  •  抹茶落季
    2020-12-12 04:23

    char* str = "string";
    

    This puts the string in read-only memory. It is undefined behavior (usually unpleasant behavior) when you try to modify it with the next line. Try something like

    char str[] = "string";
    

    instead.

提交回复
热议问题