|
|
|
|
|
|
|
To avoid unintended errors when declaring pointer variables, it is safest to declare each variable in a separate statement. |
|
|
|
|
|
|
|
|
we can make intPtr point to beta by using the unary & operator, which is called the address-of operator. At run time, the assignment statement |
|
|
|
|
|
|
|
|
takes the memory address of beta and stores it into intPtr. Alternatively, we could initialize intPtr in its declaration as follows: |
|
|
|
|
|
|
|
|
int beta;
int* intPtr = β |
|
|
|
|
|
|
|
|
Suppose that intPtr and beta happen to be located at memory addresses 5000 and 5008, respectively. Then storing the address of beta into intPtr results in the relationship pictured in Figure 17-2. |
|
|
|
|
|
|
|
|
Because actual numeric addresses are generally unknown to the C++ programmer, it is more common to display the relationship between a pointer and a pointed-to variable by using rectangles and arrows as in Figure 17-3. |
|
|
|
|
|
|
|
|
To access a variable that a pointer points to, we use the unary * operatorthe dereference or indirection operator. The expression |
|
|
|
|
|
|
|
|
denotes the variable pointed to by intPtr. In our example, intPtr currently points to beta, so the statement |
|
|
|
|
|