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

前端 未结 7 1545
礼貌的吻别
礼貌的吻别 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:11

    In the case where p="hello world"; (1st case at the time of this edit), p is being initialized to point to a read-only memory region which contains the string "hello world" (string literal). This read-only memory region is created at compile time.

    In the case that causes the segmentation fault (2nd case at the time of this edit), p is uninitialized and copying anything to it will produce unpredictable results because the location in memory that p is pointing to is not specified by the code.

    Before you can copy a string to p, you must specify the memory that p is pointing to.

    You can allocate this memory on the stack

    char buf[BUFSIZ] = ""; /* local variable */

    on the heap

    char *buf = malloc(BUFSIZ); /* don't forget to free */

    or in the __DATA segment.

    static char buf[BUFSIZ] = ""; /* global variable */

    You can then initialize p to point at the memory buffer.

    char *p = buf;

    This is similar in concept to initializing p to point to the string literal in read-only memory. Unlike the case where p points to the string literal, you can now copy a string to the character pointer as it does not point to read-only memory.

    Note: I intentionally created a separate buffer and initialized p to point to it to help make my point.

提交回复
热议问题