I started reading Pointers and while tinkering with them. I stumbled upon this :
#include<stdio.h>
int main()
{
int *p,a;
a=sizeof(*p);
printf("%d",a);
}
It outputs : 4
Then in the place of sizeof(*p)
I replaced it with sizeof(int*)
Now it outputs 8 .
P is a pointer of integer type and int* is also the same thing ( Is my assumption correct? ). Then why it is printing two different values. I am doing this on a 64bit gcc compiler.
Best Answer
Every beginner always gets confused with pointer declaration versus de-referencing the pointer, because the syntax looks the same.
int *p;
means "declare a pointer to int". You can also write it asint* p;
(identical meaning, personal preference).*p
, when used anywhere else but in the declaration, means "take the contents of what p points at".Thus
sizeof(*p)
means "give me the size of the contents that p points at", butsizeof(int*)
means "give me the size of the pointer type itself". On your machine,int
is apparently 4 bytes but pointers are 8 bytes (typical 64 bit machine).