C - SizeOf Pointers

前端 未结 4 742
旧巷少年郎
旧巷少年郎 2020-11-29 14:17
char c[] = {\'a\',\'b\',\'c\'};
int* p = &c[0];
printf(\"%i\\n\", sizeof(*p)); //Prints out 4
printf(\"%i\\n\", sizeof(*c)); //Prints out 1

I a

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 14:31

    Because p is of type int *, so *p is of type int, which is apparently 4 bytes wide on your implementation.


    And use %zu for printing size_t (what sizeof yields) if you don't want your program to invoke undefined behavior.

提交回复
热议问题