#include <stdio.h>
int main()
{
int (*ptr) [5];
printf ("%lu\n" , sizeof (ptr));
printf ("%lu\n" , sizeof (*ptr));
}
I am using 64 bit system. When I run this code in gcc compiler it generate output 8 and 20 . My question is how many byte going to allocate for this pointer to an array ? It is 8 or 20 and why?
ptrdesignates an object that will store the address of a 5-element array ofint. On your system, such a pointer takes up 8 bytes of storage.The expression
*ptrcan designate an object that is a 5-element array ofint; on your system, such an array takes up 20 bytes (5 4-byte elements).Assume the code:
This gives you something like the following (each box takes up 4 bytes):
This is equivalent to writing
The size of a pointer does not depend on the size of the thing it points to.