Sizes of arrays declared with pointers

后端 未结 5 1774
走了就别回头了
走了就别回头了 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:54

    They don't print the same because arrays and pointers are just not the same. There's no reason why they should be the same. An array is implicitly converted to a pointer in many circumstances, but this doesn't make them identical.

提交回复
热议问题