Why Can't we copy a string to Character Pointer WHEN we can assign a string directly to it?

前端 未结 7 1705
礼貌的吻别
礼貌的吻别 2021-02-10 01:16

This code produces \"p = hello world\":

#include \"stdio.h\"
#include \"string.h\"

int main(){
    char *p;
    p=\"hello world\";
    printf(\"p is %s \\n\",p)         


        
7条回答
  •  春和景丽
    2021-02-10 02:08

    Yes its annoying. You can use strdup to shorten it:

    char *p = strdup("hello");
    printf("p is %s \n",p);
    

提交回复
热议问题