Section 2 - Pointers

10A.2.1 Storing the Address of a C++ Variable

What if we want to hold the address of an object in another variable object, as in:

p = &licensePlate ;

What data type should we use for p? It has to be some kind of integer, but it must be the correct size. C++ gives us an integral type that is designed for such a use: the pointer type. street sign

Even though all addresses are the same size, and theoretically we could therefore use a single pointer type to store any one of them, C++ makes us state explicitly what kind of address we intend to store. For example, if licensePlate is an int, then we need a pointer to an int:

 

int licensePlate
int *p;

The last line says that p is a "pointer to an int." It can be written as any one of these three 

int * p;  // or ...
int* p;   // or ...
int *p;

Being a pointer to an int (also called an int pointer), p can hold the address of any int object:

int dave, mark, five;

p = &dave;
p = &mark;

A float pointer would be declared and used like this:

float *fp;
float f;

fp = &f;

In each of the above cases we say the p points to mark or fp points to f.

Let's use a pointer to print out the address of an object:

int main()
{
int licensePlate;
float stapleGun;

int *ip;
float *fp;

ip = &licensePlate;
fp = &stapleGun;

cout << "Address of the int: " << ip << endl;
cout << "Address of the float: " << fp << endl;

return 0;
}

/* ------------------- output -------------------

Address of the int: 0x03c87b4c
Address of the float: 0x03c87b48

------------------------------------------------- */

Some operating environments prepend the 0x in front of hex values when they print them to the screen.  This is to remind the user that it is a hex value.

We can declare pointers and ordinary objects in the same statement, and even initialize pointers using the address of already declared objects:

int licensePlate, *ip, x, y, *z = &x;

The address operator, &, must be applied to individual objects, not expressions or constants:

p = &x;          // good
p = &(x + y);   // error

10A.2.2 Using Pointers to Access Values

Using pointers to see the addresses of objects gets old real fast. You'd be surprised how quickly the excitement wears off. Here's something else you can do with them.  The pointer can be used to access the value of the object to which it points. Do this by using the asterisk as shown here:


int main()
{
int licensePlate;
float stapleGun;
      
int *ip;
float *fp;
      
ip = &licensePlate;
fp = &stapleGun;
     
licensePlate = 7;
cout << "The value pointed to by ip: " << *ip << endl;
cout << " (which better be the same as licensePlate) ... "
<< licensePlate  << endl << endl;
    
*fp = 1.234;
cout << "The value pointed to by fp: " << *fp << endl;
cout << " (which better be the same as stapleGun) ... "
<< stapleGun << endl << endl;
 
return 0;
}

/* ------------------- output -------------------
 
The value pointed to by ip: 7
 (which better be the same as licensePlate) ... 7
 
The value pointed to by fp: 1.234
 (which better be the same as stapleGun) ... 1.234
 
------------------------------------------------- */

By placing a * in front of the pointer, you are accessing the contents of what it points to. This is known as de-referencing the pointer..

As you can see, you can use either the original object name, or the pointer to get at the value of the object. This works for retrieving or reading  the contents, but also for writing into the contents:

cout << "The value pointed to by ip: " << *ip << endl;
*fp = 1.234; 

In fact, several pointers can point to the same object. If any one is used to modify the contents of the object pointed to, then that change will be reflected when you use any of the others to look at it.

int main()
{
float stapleGun;
float *fp, *gp;
      
fp = &stapleGun;
gp = fp;  // copying the contents of the pointer
      
*fp = 1.234;
*gp = 5.432;
     
cout << stapleGun << " " << *fp << " " << *gp << endl;
return 0;
}

/* ------------------- output -------------------
 
5.432 5.432 5.432
 
------------------------------------------------- */

If you are looking for a single example to understand the essence of elementary pointer theory, raise your eyes. This last example says it all. It shows you that you can not only use pointers to capture the addresses of ordinary objects, but they can be treated like variables themselves.

gp = fp;

This is an ordinary assignment. The contents of fp are copied into gp. It just so happens that those contents consist of the address of another object, namely stapleGun.

Again, if you can understand the last example, you will be in great shape.  It is very important. If you have trouble, you can read the pointer chapter in the text before moving on to the next section.