Size of pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Size of pointer

I have following code int *p,a; a=10; p=&a; printf ("size of p %d\n",sizeof (p)); printf ("size of p %d\n",sizeof (*p)); output: 8 4 so what size of pointer p should I consider 8 or 4 and also please answer why ?

29th May 2018, 11:22 AM
Yugabdh
Yugabdh - avatar
8 Answers
+ 1
Int is treated as 64 bit number, so 8 bytes. Pointer to int is 4 bytes.
29th May 2018, 11:37 AM
Bebida Roja
Bebida Roja - avatar
0
But if p is treated as int then why it's size is not 8 byte
29th May 2018, 12:05 PM
Yugabdh
Yugabdh - avatar
0
and what size should I consider when someone is referring to address of pointer
29th May 2018, 12:06 PM
Yugabdh
Yugabdh - avatar
0
p is a pointer to an int, not an int. Think of it this way: you need less space to store an url than the content of the page. But some machines will have 64 bit pointers
29th May 2018, 12:41 PM
Bebida Roja
Bebida Roja - avatar
0
I got it as I'm compiling on 64 bit machine address is going to b 64 bit so sizeof (p) is address where as sizeof (*p) is size of intiger a
29th May 2018, 1:28 PM
Yugabdh
Yugabdh - avatar
0
But I think sololearn uses a 64 bit compiler, so i think the pointer should also be 64 bits, but thats not the case.
29th May 2018, 1:46 PM
Bebida Roja
Bebida Roja - avatar
0
If its 64 bit then memory addresses associated with variables will also be 64 bit then 8 output is correct and pointers always have size equal to intiger type anyhow.
29th May 2018, 5:27 PM
Yugabdh
Yugabdh - avatar
0
then your question is not answered.
29th May 2018, 8:40 PM
Bebida Roja
Bebida Roja - avatar