24. What’s the Point?

Using Pointers, You’ll Find Out

image

Pointer variables, often called pointers, let you do much more with C than you can with programming languages that don’t support pointers. When you first learn about pointers, you’ll probably ask, “What’s the point?” (Even after you master them, you still might ask the same thing!) Actually, pointers provide the means for the true power of C programming. This book exposes the tip of the pointer iceberg. The concepts you learn here will form the foundation of your C programming future.

Memory Addresses

Inside your computer is a bunch of memory. The memory holds your program as it executes, and it also holds your program’s variables. Just as every house has a different address, every memory location has a different address. Not coincidentally, the memory locations have their own addresses as well. As with house addresses, the memory addresses are all unique; no two are the same.

Figure 24.1 shows you the look of a computer’s memory. The address has nothing to do with the data stored in that address. All computer addresses begin at zero and increment from there.

Figure 24.1. The computer contains lots of memory, and each memory location has a unique address.

image

As you can see from Figure 24.1, your memory acts a little like one big hardware array, with each address being a different subscript and each memory location being a different array element.

When you define variables, C finds an unused place in memory and attaches a name to that memory location. That’s a good thing. Instead of having to remember that an order number is stored at memory address 34532, you only have to remember the name orderNum (assuming you named the variable orderNum when you defined the variable). The name orderNum is much easier to remember than a number.

Clue

image

The support of variable names is just one of many programming shortcuts that compilers provide.

Warning

image

Figure 24.1 is somewhat misleading. PC addresses are often segmented into segments and offsets. The PC’s memory is divided into segments that represent sections of memory in the same way that chapters in a book represent sections of the book. The offset is a second number added to the segment’s address to get to a particular memory location. Someday we’ll program computers that use a strict flat memory model, which is a fancy way of saying we’ll be able to throw that segment stuff out the door and get back to the basics of giving every memory location a sequential address starting at 0 and continuing until the memory runs out.

Defining Pointer Variables

As with any other type of variable, you must define a pointer variable before you can use it. Before going further, you need to learn two new operators. Table 24.1 shows them along with their descriptions.

Table 24.1. The pointer operators.

image

The following shows how you would define an integer and a floating-point variable:

int num;
float value;

To define an integer pointer variable and a floating-point pointer variable, you simply insert an *:

int * pNum;  /* Defines two pointer variables */
float * pValue;

Note

image

There’s nothing special about the names of pointer variables. Lots of C programmers like to preface pointer variable names with a p, as done here, but you can name them anything you like. The p simply reminds you they are pointer variables and not regular variables.

Clue

image

All data types have corresponding pointer data types. There are character pointers, long integer pointers, and so on.

Pointer variables hold addresses of other variables. That’s their primary purpose. Use the address-of operator, &, to assign the address of one variable to a pointer. Until you assign an address of a variable to a pointer, the pointer is uninitialized and you can’t use it for anything.

The following code defines an integer variable named age and stores a 19 in age. Then a pointer named pAge is defined and initialized to point to age. The address-of operator reads just like it sounds. The second line that follows tells C to put the address of age into pAge.

int age = 19;       /* Stores a 19 in age */
int * pAge = &age;  /* Links up the pointer */

You have no idea exactly what address C will store age at. However, whatever address C uses, pAge will hold that address. When a pointer variable holds the address of another variable, it in effect points to that variable. Assuming that age is stored at the address 18826 (only C knows exactly where it is stored), Figure 24.2 shows what the resulting memory would look like.

Figure 24.2. The variable pAge points to age if pAge holds the address of age.

image

Note

image

Just because you define two variables back-to-back doesn’t mean that C stores them back-to-back in memory. C might store them together, but it also might not.

Warning

image

Never try to set the address of one type of variable to a pointer variable of a different type. C will only let you assign the address of one type of variable to a pointer defined with the same data type.

The * isn’t part of a pointer variable’s name. You will use the * dereferencing operator for several things, but in the pointer definition, the * exists only to tell C that the variable is a pointer and not a regular variable. The following four statements do exactly the same thing as the previous two statements. Notice that you don’t use * to store the address of a variable into a pointer variable unless you are also defining the pointer at the same time.

image

Using the Dereferencing *

As soon as you link up a pointer to another variable, you can work with the other value by dereferencing the pointer. Programmers never use an easy word when a hard one will do just as well (and confuse more people). Dereferencing just means that you use the pointer to get to the other variable. When you dereference, use the * dereferencing operator.

In a nutshell, here are two ways to change the value of age (assuming the variables are defined as described earlier):

age = 25;

and

*pAge = 25;  /* Stores 25 where pAge points */

Notice that you can use a variable name to store a value or dereference a pointer that points to the variable. You also can use a variable’s value in the same way. Here are two ways to print the contents of age:

printf("The age is %d. ", age);

and

printf("The age is %d. ", *pAge);

The dereferencing operator is used when a function works with a pointer variable that it is sent. In Chapter 32, you’ll learn how to pass pointers to functions. When a function uses a pointer variable that is sent from another function, you must use the dereferencing operator before the variable name everywhere it appears. The Blackjack program in Appendix B does this in the dealCard() function. The following lines from this function show the dereferencing operator being used:

image

Rewards

image

• Get comfortable with memory addresses because they form the basis of pointer usage.

• Use the & to produce the address of a variable.

• Use the * to define a pointer variable and to dereference a pointer variable. *pAge and age reference the same memory location as long as you’ve made pAge point to age.

Pitfalls

image

• Don’t try to make a pointer variable of one data type point to a variable of a different data type.

• Don’t worry about the exact address that C uses for variable storage. If you use &, C will take care of the rest.

• Don’t forget to use * when dereferencing your pointer, or you’ll get the wrong value.

• Don’t get too far ahead. You will fully appreciate pointers only after programming in C for a while. At this point (pun not intended!), pointers will not seem to help at all. The only thing you might feel a little better about is knowing what the & inside scanf() really means.

In Review

The goal of this chapter was to introduce you to pointer variables. A pointer variable is nothing more than a variable that holds the location of another variable. You can refer to the pointed-to variable by its name, or by dereferencing the pointer.

Pointers have many uses in C, especially in advanced C programming. As you’ll learn in the next chapter, arrays are nothing more than pointers in disguise. Because pointers offer more flexibility than arrays, many C programmers stop using arrays once they master pointers.

Code Example

image

Code Analysis

The code initializes two nonpointer variables—a character variable named initial and a floating-point variable named score. The code also defines two pointer variables and makes those pointer variables point to the regular variables.

As you can see in the last two statements, you can use a pointer variable to get to the contents of other variables. The printf() does not print the value of pInitial; rather, printf() prints the value of initial. The score variable, not pScore, is changed to 85.0 as well because of the dereferencing of the pointer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset