I l@ve RuBoard |
![]() ![]() |
Chapter 15. Simple Pointers
There are things, and there are pointers to things (Figure 15-1). Figure 15-1. A thing and a pointer to a thing![]() Things can come in any size; some may be big, some may be small. Pointers come in only one size (relatively small). Throughout this book I use a box to represent a thing. The box may be large or small, but things are always a box. Pointers are represented by arrows. Most novice programmers get pointers and their contents confused. To limit this problem, all pointer variables in this book end with the extension _ptr. You probably want to follow this convention in your own programs. Although not as common as it should be, this notation is extremely useful. Figure 15-1 shows one thing: a variable named thing. The name of the variable is written on the box that represents it. This variable contains the value 6. The actual address of this variable is 0x1000. C++ automatically assigns an address to each variable at compile time. The actual addresses differ from machine to machine. Most of the time you don't have to worry about variable addresses, as the compiler takes care of that detail. (After all, you've gotten through 14 chapters of programming without knowing anything about addresses.) The pointer (thing_ptr) points to the variable thing. Pointers are also called address variables since they contain the addresses of other variables. In this case, the pointer contains the address 0x1000. Since this is the address of thing, you say that thing_ptr points to thing. (You could put another address in thing_ptr and force it to point to something else.) You use "things" and "addresses" in everyday life. For example, you might live in a house (a thing). The street address might be "123 W. Main Street." An address is a small thing that can be written down on a piece of paper. Putting a house on a piece of paper is something else, requiring a lot of work and a very large crane. Street addresses are approximately the same size: one line. Houses come in various sizes. So while "1600 Pennsylvania Ave." might refer to a big house and "8347 Skid Row" might refer to a one-room shack, both addresses are the same size.[1]
Many different address variables can point to the same thing. This is true for street addresses as well. Table 15-1 lists the location of important services in a small town.
In this case you have one, large, multipurpose building that is used by several services. Although there are three address variables (Services), there is only one address (1 Main Street) pointing to one building (City Hall). As you will see in this chapter, pointers can be used as a quick and simple way to access arrays. In later chapters you will discover how pointers can be used to create new variables and complex data structures, such as linked lists and trees. As you go through the rest of the book, you will be able to understand these data structures as well as create your own. A pointer is declared by putting an asterisk (*) in front of the variable name in the declaration statement: int thing; // Define "thing" (see Figure 15-2A) int *thing_ptr; // Define "pointer to a thing" (see Figure 15-2B) Table 15-2 lists the operators used in conjunction with pointers.
The ampersand operator (&) changes a thing into a pointer. The * changes a pointer into a thing. These operators can easily cause confusion. Let's look at some simple uses of these operators in detail:
The following examples show misuse of pointer operators.
Example 15-1 illustrates a very simple use of pointers. It declares one object, thing_var, and a pointer, thing_ptr. thing_var is set explicitly by the line: thing_var = 2; The line: thing_ptr = &thing_var; causes C++ to set thing_ptr to the address of thing_var. From this point on, thing_var and *thing_ptr are the same. Example 15-1. thing/thing.cpp#include <iostream> int main( ) { int thing_var; // define a variable int *thing_ptr; // define a pointer thing_var = 2; // assigning a value to thing std::cout <<"Thing " << thing_var << '\n'; thing_ptr = &thing_var; // make the pointer point to thing *thing_ptr = 3; // thing_ptr points to thing_var so // thing_var changes to 3 std::cout << "Thing " << thing_var << '\n'; // another way of printing the data std::cout << "Thing " << *thing_ptr << '\n'; return (0); } Several pointers can point to the same thing: 1: int something; 2: 3: int *first_ptr; // One pointer 4: int *second_ptr; // Another pointer 5: 6: something = 1; // Give the thing a value 7: 8: first_ptr = &something; 9: second_ptr = first_ptr; In line 8 you use the & operator to change a simple variable (something) into a pointer that can be assigned to first_ptr. Because first_ptr and second_ptr are both pointers, you can do a direct assignment in line 9. After executing this program fragment, you have the situation illustrated by Figure 15-3. Figure 15-3. Two pointers and a thing![]() It is most important to note that while you have three variables, there is only one integer (thing). The following are all equivalent: something = 1; *first_ptr = 1; *second_ptr = 1; Finally, there is a special pointer called NULL that points to nothing. (The actual numeric value is 0.) The standard include file, stddef.h, defines the constant NULL. (Most standard include files that have anything to do with pointers automatically include NULL as well.) The NULL pointer is represented graphically in Figure 15-4. Figure 15-4. NULL![]() ![]() |
I l@ve RuBoard |
![]() ![]() |