Program aborts when using strcpy on a char pointer? (Works fine on char array)

前端 未结 6 1549
礼貌的吻别
礼貌的吻别 2021-01-02 08:23

I\'m perplexed as to why the following doesn\'t work:

char * f = \"abcdef\";
strcpy(f, \"abcdef\");
printf(\"%s\",f);

char s[] = \"ffffd\";
strcpy(&s[0],          


        
6条回答
  •  青春惊慌失措
    2021-01-02 09:14

    char * f = "abcdef"; defines a char pointer to "abcdef" which is located in read-only area so you can't write to this place

    char s[] = "ffffd"; defines a char array on the stack which is writable.

提交回复
热议问题