C++

Manipulating Data by using Pointers

Once a pointer is assigned the address of a variable, you can use that pointer to access the data in that variable. Listing 8.2 demonstrates how the address of a local variable is assigned to a pointer and how the pointer manipulates the values in that variable.

Listing 8.2. Manipulating data by using pointers.

1: // Listing 8.2 Using pointers

2:

3: #include <iostream.h>

4:

5: typedef unsigned short int USHORT;

6: int main()

7: {

8: USHORT myAge; // a variable

9: USHORT * pAge = 0; // a pointer

10: myAge = 5;

11: cout << "myAge: " << myAge << "\n";

12:

13: pAge = &myAge; // assign address of myAge to pAge

14:

15: cout << "*pAge: " << *pAge << "\n\n";

16:

17: cout << "*pAge = 7\n";

18:

19: *pAge = 7; // sets myAge to 7

20:

21: cout << "*pAge: " << *pAge << "\n";

22: cout << "myAge: " << myAge << "\n\n";

23:

24:

25: cout << "myAge = 9\n";

26:

27: myAge = 9;

28:

29: cout << "myAge: " << myAge << "\n";

30: cout << "*pAge: " << *pAge << "\n";

31:

32: return 0;

33: }

Output:

myAge: 5

*pAge: 5

*pAge = 7

*pAge: 7

myAge: 7

myAge = 9

myAge: 9

*pAge: 9

Analysis: This program declares two variables: an unsigned short, myAge, and a pointer to an unsigned short, pAge. myAge is assigned the value 5 on line 10; this is verified by the printout in line 11.

On line 13, pAge is assigned the address of myAge. On line 15, pAge is dereferenced and printed, showing that the value at the address that pAge stores is the 5 stored in myAge. In line 17, the value 7 is assigned to the variable at the address stored in pAge. This sets myAge to 7, and the printouts in lines 21-22 confirm this.

In line 27, the value 9 is assigned to the variable myAge. This value is obtained directly in line 29 and indirectly (by dereferencing pAge) in line 30.

Examining the Address

Pointers enable you to manipulate addresses without ever knowing their real value. After this unit, you’ll take it on faith that when you assign the address of a variable to a pointer, it really has the address of that variable as its value. But just this once, why not check to make sure? Listing 8.3 illustrates this idea.

Listing 8.3. Finding out what is stored in pointers.

1: // Listing 8.3 What is stored in a pointer.

2:

3: #include <iostream.h>

4:

5: typedef unsigned short int USHORT;

6: int main()

7: {

8: unsigned short int myAge = 5, yourAge = 10;

9: unsigned short int * pAge = &myAge; // a pointer

10:

11: cout << "myAge:\t" << myAge << "\tyourAge:\t" << yourAge << "\n";

12: cout << "&myAge:\t" << &myAge << "\t&yourAge:\t" << &yourAge <<"\n";

13:

14: cout << "pAge:\t" << pAge << "\n";

15: cout << "*pAge:\t" << *pAge << "\n";

16:

17: pAge = &yourAge; // reassign the pointer

18:

19: cout << "myAge:\t" << myAge << "\tyourAge:\t" << yourAge << "\n";

20: cout << "&myAge:\t" << &myAge << "\t&yourAge:\t" << &yourAge <<"\n";

21:

22: cout << "pAge:\t" << pAge << "\n";

23: cout << "*pAge:\t" << *pAge << "\n";

24:

25: cout << "&pAge:\t" << &pAge << "\n";

26: return 0;

27: }

Output: 

myAge: 5 yourAge: 10

&myAge: 0x355C &yourAge: 0x355E

pAge: 0x355C

*pAge: 5

myAge: 5 yourAge: 10

&myAge: 0x355C &yourAge: 0x355E

pAge: 0x355E

*pAge: 10

&pAge: 0x355A

(Your output may look different.)

Analysis: In line 8, myAge and yourAge are declared to be variables of type unsigned short integer. In line 9, pAge is declared to be a pointer to an unsigned short integer, and it is initialized with the address of the variable myAge. Lines 11 and 12 print the values and the addresses of myAge and yourAge. Line 14 prints the contents of pAge, which is the address of myAge. Line 15 prints the result of dereferencing pAge, which prints the value at pAge—the value in myAge, or 5.

This is the essence of pointers. Line 14 shows that pAge stores the address of myAge, and line 15 shows how to get the value stored in myAge by dereferencing the pointer pAge. Make sure that you understand this fully before you go on. Study the code and look at the output.

In line 17, pAge is reassigned to point to the address of yourAge. The values and addresses are printed again. The output shows that pAge now has the address of the variable yourAge and that dereferencing obtains the value in yourAge.

Line 25 prints the address of pAge itself. Like any variable, it has an address, and that address can be stored in a pointer. (Assigning the address of a pointer to another pointer will be discussed shortly.)

DO use the indirection operator (*) to access the data stored at the address in a pointer. DO initialize all pointers either to a valid address or to null (0). DO remember the difference between the address in a pointer and the value at that address.

Back to Index