In C++, there is a concept of a pointer to an array, where it is said to point to the “whole array” rather than just the first element. This idea confuses me. How can a pointer point to an entire array? Shouldn’t it only point to the first element?
10
Lets draw it out for you…
First we have out array:
+--------+--------+-----+--------+ | arr[0] | arr[1] | ... | arr[9] | +--------+--------+-----+--------+
Now lets add some pointers:
+--------+--------+-----+--------+ &arr --> | arr[0] | arr[1] | ... | arr[9] | +--------+--------+-----+--------+ ^ ^ ^ | | | &arr[0] &arr[1] &arr[9]
Here we can see that &arr
will point to the array, but it’s a pointer to the whole array, not a pointer to a single element like &arr[0]
.
To perhaps show it in another way: If you don’t know, then arr[i]
is exactly the same as *(arr + i)
. That is, you take a pointer to the first element and add the index to get a pointer to that element, then dereference it.
The important part here is that arr + 1
is a pointer to an element.
Now lets say we store the pointer to the array in a variable:
int (*arr_ptr)[10] = &arr;
With this we know that arr_ptr[0]
is the same as *(arr_ptr + 0)
which is the same as *arr_ptr
.
What we also know that arr_ptr[1]
will be the same as *(arr_ptr + 1)
.
Lets take the drawing above, and add the pointers arr_ptr
and arr_ptr + 1
:
+--------+--------+-----+--------+ arr_ptr --> | arr[0] | arr[1] | ... | arr[9] | ... unknown memory +--------+--------+-----+--------+ ^ ^ ^ ^ | | | | &arr[0] &arr[1] &arr[9] arr_ptr + 1
This drawing shows that arr_ptr
(and thus &arr
) is a pointer to the whole array.
6
What really changes is the behavior of pointer arithmetic, incrementing/decrementing or adding/subtracting an offset to the pointer, because they are two different types of pointers.
#include <stdio.h>
int main()
{
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
void *arrPlusOne = (void *)(&arr+1);
void *arrItemPlusOne = (void *)(&arr[0]+1);
printf("%p %p %p %pn", (void *)&arr, (void *)&(arr[0]), arrPlusOne, arrItemPlusOne);
printf("array pointer step: %ldn", ((char *)arrPlusOne - (char *)&arr));
printf("array item pointer step: %ldn", ((char *)arrItemPlusOne - (char *)&arr));
return 0;
}
The output might be something like this:
0x7fff4b58ce80 0x7fff4b58ce80 0x7fff4b58cea8 0x7fff4b58ce84
array pointer step: 40
array item pointer step: 4
The cast to char pointer is to allow arithmetic subtraction which is not allowed on void pointers.
Cast to void pointers in printf()
is required because is a variadic function.
If you run this code you will see that the third and fourth pointers point to different memory locations even though the code always adds 1, depending on the processor architecture and type sizes.
0
Much like a class
/struct
can be thought of as a container of data members, an array can be thought of as a container of elements (and in fact, std::array
is part of the standard Containers library).
You can have a pointer to a single member/element, and you can have a pointer to the entire container as a whole. Both pointers may point at the same address in memory, but the things they are pointing at are logically and semantically different things.
A pointer tells you not only WHERE it is pointing at, but WHAT it is pointing at.
It will become evident from the following test:
#include <stdio.h>
int main() {
int a[10];
printf("%lu, %lu", sizeof(a), sizeof(a[0]));
return 0;
}
Output is:
40, 4
Therefore, an int
array of 10 elements have a size of 40, whereas a single int
has a size of 4. Therefore the size of your array is:
size_of_unit * number_of_units