Arrays and Pointers in C++

A C++ array name is actually a constant pointer to the first element in the array.

An array without a bracket and a subscript actually represents the starting address of the array. In this sense, an array is essentially a pointer. Suppose you declare an array of int values as follows:

int list[6] = {11, 12, 13, 14, 15, 16};

The following statement displays the starting address of the array:

cout << “The starting address of the array is ” << list << endl;

Figure 11.3 shows the array in the memory. C++ allows you to access the elements in the array using the dereference operator. To access the first element, use *list. Other elements can be accessed using *(list + 1), *(list + 2), *(list + 3), *(list + 4), and *(list + 5).

An integer may be added to or subtracted from a pointer. The pointer is incremented or decremented by that integer times the size of the element to which the pointer points.

Array list points to the starting address of the array. Suppose this address is 1000. Will list + 1 be 1001? No. It is 1000 + sizeof(int). Why? Since list is declared as an array of int elements, C++ automatically calculates the address for the next element by add­ing sizeof(int). Recall that sizeof(type) function returns the size of a data type (see Section 2.8, “Numeric Data Types and Operations”). The size of each data type is machine dependent. On Windows, the size of the int type is usually 4. So, no matter how big each element of the list is, list + 1 points to the second element of the list, and list + 3 points to the third, and so on.

Listing 11.2 gives a complete program that uses pointers to access array elements.

Listing 11.2 ArrayPointer.cpp

1 #include <iostream>
2
using namespace std;
3
4
int main()
5 {
6   
int list[6] = {11, 12, 13, 14, 15, 16};
7
8   
for (int i = 0; i < 6; i++)
9    cout <<
“address: ” << (list + i) <<
10   
” value: ” << *(list + i) << ” ” <<
11   
” value: ” << list[i] << endl;
12
13   
return 0;
14 }

As shown in the sample output, the address of the array list is 0013FF4C. So (list + 1) is actually 0013FF4C + 4, and (list + 2) is 0013FF4C + 2 * 4 (line 9). The array elements are accessed using pointer dereference *(list + 1) (line 10). Line 11 accesses the elements via index using list[i], which is equivalent to *(list + i).

Arrays and pointers form a close relationship. A pointer for an array can be used just like an array. You can even use pointer with index. Listing 11.3 gives such an example.

Listing 11.3 PointerWithIndex.cpp

1 #include <iostream>
2
using namespace std;
3
4
int main()
5 {
6   
int list[6] = {11, 12, 13, 14, 15, 16};
7   
int* p = list;
8
9   
for (int i = 0; i < 6; i++)
10     cout <<
“address: ” << (list + i) <<
11       
” value: ” << *(list + i) << ” ” <<
12       
” value: ” << list[i] << ” ” <<
13       
” value: ” << *(p + i) << ” ” <<
14       
” value: ” << p[i] << endl;
15
16   
return 0;
17 }

Line 7 declares an int pointer p assigned with the address of the array.

int* p = list;

Note that we do not use the address operator (&) to assign the address of the array to the pointer, because the name of the array is already the starting address of the array. This line is equivalent to

int* p = &1ist[0];

Here, &list[0] represents the address of list[0].

As seen in this example, for array list, you can access an element using array syntax list[i] as well as pointer syntax *(list + i). When a pointer such as p points to an array, you can use either pointer syntax or the array syntax to access an element in the array—i.e., *(p + i) or p[i]. You can use array syntax or pointer syntax to access arrays, whichever is convenient. However, there is one difference between arrays and pointers. Once an array is declared, you cannot change its address. For example, the following statement is illegal:

int list1[10], list2[10];

list1 = list2; // Wrong

An array name is actually treated as a constant pointer in C++.

C-strings are often referred to as pointer-based strings, because they can be conveniently accessed using pointers. For example, the following two declarations are both fine:

char city[7] = “Dallas”; // Option 1

char* pCity = “Dallas”;   // Option 2

Each declaration creates a sequence that contains characters ‘D’, ‘a’, ‘l’, ‘l’, ‘a’, ‘s’, and ‘\0’.

You can access city or pCity using the array syntax or pointer syntax. For example, each of the following

cout << city[1] << endl;

cout << *(city + 1) << endl;

cout << pCity[1] << endl;

cout << *(pCity + 1) << endl;

displays character a (the second element in the string).

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *