Sizes of arrays declared with pointers

后端 未结 5 1795
走了就别回头了
走了就别回头了 2021-01-23 14:24
char c[] = \"Hello\";
char *p = \"Hello\";

printf(\"%i\", sizeof(c)); \\\\Prints 6
printf(\"%i\", sizeof(p)); \\\\Prints 4

My question is:

Why

5条回答
  •  忘掉有多难
    2021-01-23 14:55

    pointer and array are different.

    char c[] = "Hello"; //declare c as an array. sizeof(c) calculate the size of array 6.
    char *p = "Hello"; //declare p as a pointer.sizeof(p) calculate the size of pointer,in most machine the size of pointer is 4.

    you can also refer to .

提交回复
热议问题