|
|
|
|
|
|
|
New Term: A pointer is a variable that holds a memory address. |
|
|
|
|
|
|
|
|
Stop. Read that again. A pointer is a variable. You know what a variable is; it is an object that can hold a value. An integer variable holds a number. A character variable holds a letter. A pointer is a variable that holds a memory address. |
|
|
|
|
|
|
|
|
Okay, so what is a memory address? To fully understand this, you must know a little about computer memory. Don't panic; it isn't very difficult. |
|
|
|
|
|
|
|
|
Computer memory is where these values are stored. By convention, computer memory is divided into sequentially numbered memory locations. Each of these locations is a memory address. |
|
|
|
|
|
|
|
|
Every variable of every type is located at a unique location in an address. Figure 9.1 shows a schematic representation of the storage of an unsigned long integer variable, the Age. |
|
|
|
|
|
|
|
|
FIGURE 9.1
A schematic representation of the Age. |
|
|
|
|
|
|
|
|
Different computers number this memory using different complex schemes. Usually, programmers don't need to know the particular address of any given variable because the compiler handles the details. If you want this information, though, you can use the addressof operator (&), which is illustrated in Listing 9.1. |
|
|
|
|
|
|
|
|
LISTING 9.1 DEMONSTRATING THE ADDRESSES OF VARIABLES |
|
|
|
 |
|
|
|
|
1: // Listing 9.1 Demonstrates address of operator
2: // and addresses of local variables
3:
4: #include <iostream.h> |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|