Understanding Pointers 227
8
Pointers, Addresses, and Variables ................................................................
It is important to distinguish between a pointer, the address that the pointer holds, and
the value at the address held by the pointer. This is the source of much of the confusion
about pointers.
Consider the following code fragment:
int theVariable = 5;
int * pPointer = &theVariable ;
theVariableis declared to be an integer variable initialized with the value 5. pPointer
is declared to be a pointer to an integer; it is initialized with the address of theVariable.
pPointeris the pointer. The address that pPointerholds is the address of theVariable.
The value at the address that pPointerholds is 5. Figure 8.2 shows a schematic repre-
sentation of theVariableand pPointer.
// make a pointer to an unsigned short
unsigned short * pAge = 0;
When the pointer is dereferenced, the dereference (or indirection) operator indicates
that the value at the memory location stored in the pointer is to be accessed, rather than
the address itself.
// assign 5 to the value at pAge
*pAge = 5;
Also note that this same character (*) is used as the multiplication operator. The compiler
knows which operator to call based on how you are using it (context).
FIGURE8.2
A schematic
representation
of memory.
5
0000
100 101 102 103 104 105 106 107 108 109
000000000101 00000000000000000000000001100101
101
Address location
theVariable pPointer
In Figure 8.2, the value 5 is stored at address location 101. This is shown in the binary
number
0000 0000 0000 0101