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
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.
sizeof(*p) will print size of p which is 4 because of int but c is of char that's why it is 1
sizeof(*p)
is the size of the int
object to which p
points.
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.