Understanding array names and pointers
As we have seen, elements of arrays can always be accessed via indices and traversed by means of an integer offset from the zeroth element. Sometimes, however, it is more convenient to access array elements via a pointer equivalent.
Let's begin by declaring an array and two pointers, as follows:
const int arraySize = 5;
int array[5] = { 1 , 2 , 3 , 4 , 5 };
int* pArray1 = NULL;
int* pArray2 = NULL;
We have declared a contiguous block of arraySize
, or 5
, which are elements that are integers. We don't use arraySize
in the array declaration because that would make the array a variable-length array (VLA) that cannot be initialized in this way. We have also declared two pointers to integers—pArray1
and pArray2
. In the memory on my...