8.9 Relationship Between Pointers and Built-In Arrays

Built-in arrays and pointers are intimately related in C++ and may be used almost interchangeably. Pointers can be used to do any operation involving array subscripting.

Assume the following declarations:


int b[ 5 ]; // create 5-element int array b; b is a const pointer
int* bPtr; // create int pointer bPtr, which isn't a const pointer

We can set bPtr to the address of the first element in the built-in array b with the statement


bPtr = b; // assign address of built-in array b to bPtr

This is equivalent to assigning the address of the first element as follows:


bPtr = &b[0]; // also assigns address of built-in array b to bPtr

The expression


b += 3

causes a compilation error, because it attempts to modify the value of the built-in array’s name with pointer arithmetic.

8.9.1 Pointer/Offset Notation

Built-in array element b[3] can alternatively be referenced with the pointer expression


*(bPtr + 3)

The 3 in the preceding expression is the offset to the pointer. When the pointer points to the beginning of a built-in array, the offset indicates which built-in array element should be referenced, and the offset value is identical to the subscript. This notation is referred to as pointer/offset notation. The parentheses are necessary, because the precedence of * is higher than that of +. Without the parentheses, the preceding expression would add 3 to a copy of *bPtr’s value (i.e., 3 would be added to b[0], assuming that bPtr points to the beginning of the built-in array).

Just as the built-in array element can be referenced with a pointer expression, the address


&b[3]

can be written with the pointer expression


bPtr + 3

8.9.2 Pointer/Offset Notation with the Built-In Array’s Name as the Pointer

The built-in array name can be treated as a pointer and used in pointer arithmetic. For example, the expression


*(b + 3)

also refers to the element b[3]. In general, all subscripted built-in array expressions can be written with a pointer and an offset. In this case, pointer/offset notation was used with the built-in array’s name as a pointer. The preceding expression does not modify the built-in array’s name; b still points to the built-in array’s first element.

8.9.3 Pointer/Subscript Notation

Pointers can be subscripted exactly as built-in arrays can. For example, the expression


bPtr[1]

refers to b[1]; this expression uses pointer/subscript notation.

Good Programming Practice 8.3

For clarity, use built-in array notation instead of pointer notation when manipulating built-in arrays.

8.9.4 Demonstrating the Relationship Between Pointers and Built-In Arrays

Figure 8.17 demonstrates the four notations we just discussed:

  • array subscript notation

  • pointer/offset notation with the built-in array’s name as a pointer

  • pointer subscript notation

  • pointer/offset notation with a pointer

to accomplish the same task, namely displaying the four elements of the built-in array of ints named b.

Fig. 8.17 Using subscripting and pointer notations with built-in arrays.

Alternate View

 1   // Fig. 8.17: fig08_17.cpp
 2   // Using subscripting and pointer notations with built-in arrays.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main() {
 7      int b[]{10, 20, 30, 40}; // create 4-element built-in array b
 8      int* bPtr{b}; // set bPtr to point to built-in array b
 9
10      // output built-in array b using array subscript notation
11      cout << "Array b displayed with:

Array subscript notation
";
12
13      for (size_t i{0}; i < 4; ++i) {
14         cout << "b[" << i << "] = " << b[i] << '
';
15      }
16
17      // output built-in array b using array name and pointer/offset notation
18      cout << "
Pointer/offset notation where "
19         << "the pointer is the array name
";
20
21      for (size_t offset1{0}; offset1 < 4; ++offset1) {
22         cout << "*(b + " << offset1 << ") = " << *(b + offset1) << '
';
23      }
24
25     // output built-in array b using bPtr and array subscript notation
26     cout << "
Pointer subscript notation
";
27
28     for (size_t j{0}; j < 4; ++j) {
29        cout << "bPtr[" << j << "] = " << bPtr[j] << '
';
30     }
31
32     cout << "
Pointer/offset notation
";
33
34     // output built-in array b using bPtr and pointer/offset notation
35     for (size_t offset2{0}; offset2 < 4; ++offset2) {
36        cout << "*(bPtr + " << offset2 << ") = "
37           << *(bPtr + offset2)   << '
';
38     }
39   }

Array b displayed with:

Array subscript notation
b[0] = 10
b[1] = 20
b[2] = 30
b[3] = 40

Pointer/offset notation where the pointer is the array name
*(b + 0) = 10
*(b + 1) = 20
*(b + 2) = 30
*(b + 3) = 40

Pointer subscript notation
bPtr[0] = 10
bPtr[1] = 20
bPtr[2] = 30
bPtr[3] = 40

Pointer/offset notation
*(bPtr + 0) = 10
*(bPtr + 1) = 20
*(bPtr + 2) = 30
*(bPtr + 3) = 40
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset