< previous page page_970 next page >

Page 970
int* p, q;
as if it were written
int* p;
int  q;
To avoid unintended errors when declaring pointer variables, it is safest to declare each variable in a separate statement.
Given the declarations
int beta;
int* intPtr;
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
intPtr = &beta;
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 = &beta;
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
*intPtr
denotes the variable pointed to by intPtr. In our example, intPtr currently points to beta, so the statement
*intPtr = 28;

 
< previous page page_970 next page >