C - SizeOf Pointers

前端 未结 4 619
旧巷少年郎
旧巷少年郎 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.

    0 讨论(0)
  • 2020-11-29 14:35

    sizeof(*p) will print size of p which is 4 because of int but c is of char that's why it is 1

    0 讨论(0)
  • 2020-11-29 14:38

    sizeof(*p) is the size of the int object to which p points.

    0 讨论(0)
  • 2020-11-29 14:54

    In C (not C99) the sizeof operator is strictly a compile time calculation. so when sizeof (*p) [a dereferenced ptr to an integer] is evaluated then the size of int is four.

    Note. the (*p) portion of that statement is a "cast" operator. So, sizeof is NOT a function call as in sizeof(xyz), rather it is like sizeof var_name or sizeof (int *).

    When your program runs it changes the object pointed to by p, but the value of sizeof (*p) was already computed and hard-coded into your executable load module.

    I think your confusion is that you were thinking that C would figure out what data type p was pointing to when your program runs.

    0 讨论(0)
提交回复
热议问题