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

前端 未结 7 1546
礼貌的吻别
礼貌的吻别 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 01:52

    There is no free lunch - you need to grab & manage memory. If you just assume that because you have access to a pointer memory should be there then you'll run into unspecified behavior (segfault likely).

提交回复
热议问题