Pointer to array of elements when dereferenced return an address. Since it is holding the address of the first element of the array, dereferencing it should return a value.
The question was
"Why does
*ptr
return the base address of the array, shouldn't it return the value at that address?"
It does return the value at that address, which is the array arr
.
Think about a queue of people: you can point to the first person and say "that person over there" or you can point to the same person and say "that queue over there". There are 2 things at the same location: a person and a queue. Same thing happens with arrays: person *
for that "person over there" and person (*)[42]
"for that queue of 42 people". If you dereference a pointer to queue, you get a queue. If you take the first from the queue you get a person.
But then the array itself will decay to address to the first element when it is given as an argument to printf
. Thus here,
int arr[] = { 3, 5, 6, 7, 9 };
int (*ptr)[5] = &arr;
// undefined behaviour really, all pointers should be cast to void *
printf("%p %p %p %p", *ptr, &ptr[0], arr, &arr[0]);
all these 4 expressions will result in pointer to int
, and the value is the address of the first element in the array (the address of 3 in arr
).