Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Dereferencing Pointers

A variable of type tex2html_wrap_inline73359 is said to point at an object of type T. What this really means is that the value of the tex2html_wrap_inline73359 variable is the address of another variable of type T. For example, in Figure gif the variable q is has type int*, i.e., it is a pointer to an int. The value of p is 1004, which is the address of the variable j.

To access the variable to which a pointer points, we must dereference  the pointer. E.g., to dereference the pointer q we write *q. Whereas q denotes the pointer variable itself, *q denotes the int to which the pointer points.

If we use *q in a context where an r-value is expected, then we get the r-value of the variable to which q points. E.g., since q points to the variable j, the assignment

i = *q;
takes the r-value of j (31) and assigns it to i.

Similarly, if we use *q in a context where an l-value is required, then we get the l-value of the variable to which q points. E.g., since q points to the variable j, the assignment

*q = 31;
takes the l-value of j (1004) and stores 31 at that location. Notice that the l-value of *q is identical to the r-value of q.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.