5.11.3 Arrays Are Pointers


An array is a pointer

C++ has an unusual way of treating arrays, and it takes a while to get used to it. The critical rule to remember is that, in C++:

An array is considered to be the same thing as a pointer to the first item in the array.
That rule has several consequences.

An array of integers has type int*.

C++ separates the issue of allocating an array from the issue of using an array. Once you have an array, what you actually have is a pointer to the first thing in the array. C++ does not distinguish between a pointer to one variable and a pointer to a whole array of variables! The presence of other things after the first one in the memory is considered an allocation issue.

So an array of ints has type int*, an array of characters has type char*, etc. There is no way for you to know just by looking at the type whether the pointer points to a single variable or to an array. When it does point to an array, there is no way for you to determine the size of the array. You need to obtain that information separately.


Pointer arithmetic

Suppose that p is a pointer of type T*, where T is a type. Keep in mind that a pointer is a memory address, which is just an integer from the computer's perspective. If k is an integer, then C++ expression p + k is defined to be memory address p + k*sizeof(T), where sizeof(T) is the number of bytes that something of type T occupies. For example, if p points to the first member of an array of ints, where an int occupies 4 bytes, then p+1 points to the second variable in that array, p+2 points to the third variable, etc. (If p is memory address 1000 then p+1 is memory address 1004, p+2 is memory address 1008, etc.)

p[k]

Notation p[k] abbreviates *(p+k). That is, it (1) gets the address of the variable at index k in the array that is pointed to by p, then (2) gets the variable at index k through that pointer.

Suppose that p has type int*. Then p+1 also has type int*. So expression *(p+1) has type int. Since p[1] abbreviates *(p+1), p[1] has type int. It is the item in array p at index 1.


Getting a pointer to an array

An array is a pointer, and you can store that pointer into any pointer variable of the correct type. For example,
  int  A[10];
  int* p = A;
  p[0] = 0;
makes variable p point to the first member of array A. Setting p[0] = 0 is equivalent to setting A[0] = 0, since pointers p and A are the same.

Be careful to notice that p points into the run-time stack frame of the current function, since array A has been allocated in the run-time stack. As soon as the function returns, that memory is no longer yours to use.



Exercises

  1. Is the following allowed?

      long  B[20];
      long* p = B;
    
    Answer

  2. What does A[i] abbreviate? Answer

  3. Suppose that p has type int*. Is the following allowed?

      int k = p[4];
    
    Answer

  4. Suppose that you are using a 64-bit machine and a variable of type long occupies 8 bytes. If pointer variable p holds memory address 2000, what memory address does expression p+2 yield? Answer