// pointer of array
#include<stdio.h>
int main()
{
int arr[] = { 3, 5, 6, 7, 9 };
int *p = arr;
int (*ptr)[5] = &arr;
printf("p = %p, ptr = %p\n", p, ptr);
printf("*p = %d, *ptr = %p\n", *p, *ptr);
printf("sizeof(p) = %lu, sizeof(*p) = %lu\n",
sizeof(p), sizeof(*p));
printf("sizeof(ptr) = %lu, sizeof(*ptr) = %lu\n",
sizeof(ptr), sizeof(*ptr));
return 0;
}
Output
p = 0x7ffde1ee5010, ptr = 0x7ffde1ee5010
*p = 3, *ptr = 0x7ffde1ee5010
sizeof(p) = 8, sizeof(*p) = 4
sizeof(ptr) = 8, sizeof(*ptr) = 20
Here why sizeof(p) or sizeof(ptr) is 8?
This code is an example of pointer to array
Why 8 and why not 4 ?
>Solution :
On a 64-bit system, which yours evidently is, pointers are represented by 8 bytes, regardless of what they point to.
In your example, both p and ptr are pointers. In the case of p, we have a pointer to the first int in the array. On your system evidently int is 4 bytes. In the case of ptr, it’s been declared as a pointer to an array of 5 int values, which is 20 bytes.