Download C++: A Beginner`s Guide, Second Edition

Transcript
Indexing a Pointer
As you have just seen, it is possible to access an array using pointer arithmetic. What you might find
surprising is that the reverse is also true. In C++, it is possible to index a pointer as if it were an array.
Here is an example. It is a third version of the case-changing program.
The program creates a char pointer called p and then assigns to that pointer the address of the first
element in str. Inside the for loop, p is indexed using the normal array indexing syntax. This is perfectly
valid because in C++, the statement p[i] is functionally identical to *(p+i). This further illustrates the
close relationship between pointers and arrays.
32
C++ A Beginner’s Guide by Herbert Schildt