|
|
|
|
|
|
|
Figure 17-5
Formal Parameter Pointing to an Actual Parameter |
|
|
|
|
|
|
|
|
Indexing a pointer variable is made possible by the following rule in C++: |
|
|
|
|
|
|
|
|
Indexing is valid for any pointer expression, not only an array name. (Indexing a pointer only makes sense, though, if the pointer points to an array.) |
|
|
|
|
|
|
|
|
We have now seen four C++ operators that are valid for pointers: =, *, ->, and []. The following table lists the most common operations that may be applied to pointers. |
|
|
|
|
| Operator | Meaning | Example | Remarks | | = | Assignment | ptr = &someVar;
ptr1 = ptr2;
ptr = 0; | Except for the null pointer, both operands must be of the same data type. | | * | Dereference | *ptr | | | ==, !=, <, <=, >, and >= | Relational operators | ptr1 == ptr2 | The two operands must be of the same data type. | | ! | Logical NOT | !ptr | The result is 1 if the operand is 0 (the null pointer), else the result is 0. | | [] | Index (or subscript) | ptr[4] | The indexed pointer should point to an array. | | -> | Member selection | ptr->height | Selects a member of the class, struct, or union variable that is pointed to. |
|
|
|