Chapter 15. Simple Pointers

I l @ ve RuBoard

The choice of a point of view is the initial act of culture.

”Ortega y Gasset

There are things, and there are pointers to things (Figure 15-1).

Figure 15-1. A thing and a pointer to a thing
figs/c++2_1501.gif

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]

[1] For readers not familiar with American culture, 1600 Pennsylvania Ave. is the address of the President's mansion, while 8347 Skid Row is the typical address of a very poor family.

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.

Table 15-1. Small-town directory

Service (variable name)

Address (address value)

Building (thing)

Fire department

1 Main Street

City Hall

Police station

1 Main Street

City Hall

Planning office

1 Main Street

City Hall

Gas station

2 Main Street

Ed's Gas Station

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.

Table 15-2. Pointer operators

Operator

Meaning

*

Dereference (given a pointer, get the thing referenced)

&

Address of (given a thing, point to it)

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:

thing

A thing. The declaration int thing does not contain an asterisk, so thing is not a pointer. For example:

 thing = 4; 
&thing

A pointer to thing . thing is an object. The & (address of) operator gets the address of an object (a pointer), so &thing is a pointer. For example:

 thing_ptr = &thing;      // Point to the thing                               // (See Figure 15-2A) *thing_ptr = 5;              // Set "thing" to 5                               // (See Figure 15-2B) 
Figure 15-2. Pointer operators
figs/c++2_1502.gif
thing_ptr

Thing pointer. The asterisk ( * ) in the declaration indicates this is a pointer. Also, you have put the extension _ptr onto the name.

*thing_ptr

A thing. The variable thing_ptr is a pointer. The * (dereference operator) tells C++ to look at the data pointed to, not at the pointer itself. Note that this points to an integer, any integer. It may or may not point to the specific variable thing .

 *thing_ptr = 5;              // Assign 5 to an integer                              // We may or may not be pointing                                 to the specific integer "thing" 

The following examples show misuse of pointer operators.

*thing

Illegal. Asks C++ to get the object pointed to by the variable thing . Since thing is not a pointer, this is an invalid operation.

&thing_ptr

Legal, but strange . thing_ptr is a pointer. The & (address of) operator gets a pointer to the object (in this case thing_ptr ). The result is a pointer to a pointer. (Pointers to pointers do occur in more complex programs.)

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
figs/c++2_1503.gif

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
figs/c++2_1504.gif
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net