Dereferencing Pointer Variables

So far we've used the address-of operator to find the memory addresses of variables. These values have also been stored in pointer variables. Addresses themselves are not really that interesting, though; in the end we want to work with the actual data stored in memory at those addresses. C provides the dereferencing operator (*) to do just that.

Given a pointer variable that contains an address, the * operator allows you to read and write the value stored at the place in memory whose address is stored in the pointer variable. This process is called dereferencing a pointer.

The dereferenced address can be used wherever the original variable could be. So if x is an integer pointer variable, then *x can be used wherever an integer is expected.

As always, an example will make this clearer.

To use the * operator

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 9.4):

/* pointer4.c - Script 9.4 */
#include <stdio.h>

Script 9.4. The dereferencing operator (*) is required to access a variable's value through its address.


3.
Begin the main function:

int main (void) {

4.
Define and initialize an integer, a, and an integer pointer variable, x:

int a = 456;
int *x;

Once again, we are using these variables for demonstration purposes, hence their trivial names and values. Make sure, though, that the variable type of the pointer matches the type of the variable to which it points.

5.
Store and print the address of the integer a:

x = &a;
printf("Address of a: %p
", x);

First, the pointer x is assigned the address of the variable a. Then this pointer value is printed.

6.
Print the initial value of a:

printf("a at start: %d
", a);

Through dereferencing, this application will change the value of a, so its initial value is printed for comparison.

7.
Use the dereferencing operator to access a's value through the address stored in pointer variable x:

printf("*x at start: %d
", *x);

Since x is a pointer variable whose value is the address of a, printing the value of *x will be the same as printing the value of a.

8.
Assign a new value to a through the address stored in x:

*x = 123;

This is the equivalent of writing

a = 123;

Note how a and *x are interchangeable after they have been linked by the x = &a statement. This application accesses a place in memory directly by its name a and indirectly through its address stored in the pointer variable x, with a little help from the dereferencing operator.

9.
Print the value of a again, using both the variable and its pointer:

printf("a at end: %d
", a);
printf("*x at end: %d
", *x);

These lines will confirm that the change was made to a's value. Again, both a and *x refer to that same value.

10.
Complete the main function:

    getchar();
    return 0;
}

11.
Save the file as pointer4.c, compile, and debug as necessary.

12.
Run the application (Figure 9.9).

Figure 9.9. A variable can be changed using its address and the dereferencing operator (*).


✓ Tips

  • This example illustrates an important point: Using *x, you are able to read and change a variable without knowing its name. Later in the chapter, this idea will be put to good use when we pass the addresses of variables to a function instead of their values.

  • To clear up the terminology being used, a quick reiteration of this chapter's concepts is in order. First, the address is the physical location, in a computer's memory, where the value of a variable is stored. A pointer variable is a special type of variable that can be assigned an address. A pointer, a term you'll commonly see, is simply another word for address.

  • C has two uses for the asterisk. Referring to *var is dereferencing, or accessing the value stored in the variable var points to. You may also see a construct like this, where a pointer variable is defined and initialized in one step:

    int *x = &a;
    

    It is very important to realize that the asterisk in such a line does not act as a dereferencing operator (even though it looks like a dereferencing operation). Rather, this asterisk is part of the pointer variable's type, together with int. Written in two lines it would look like this:

    int *x;
    x = &a;
    

Using Multiple Pointers to the Same Location

The examples so far used only one pointer variable to store the address of some other variable. You can, of course, have multiple pointer variables that store the same address.

If you declare

int a = 456;
int *x = &a;
int *y = &a;
int *z = &a;

then a, or the dereferenced *x, *y, and *z refer to the same value (Figure 9.10). If you change the value stored in a by using any one of them (like *x = 24;), the change will also be visible when you read the value through any of the others.

Figure 9.10. Multiple pointer variables can contain the same address of, and therefore point to, the same location in memory.



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

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