Pointers are exactly what they say they are. They point.
They point to the location of another variable in memory. Why use them then? You've probably already used them without even knowing it.
Defining pointers:
| char variable = 'G'; //a variable |
| char * pvariable; //a pointer |
//The p is not necessary, it just helps us to remember that it's a pointer.
| pvariable = &variable; //links the pointer to the variable. |
| cout << *pvariable; //prints "G" |
Ok. You're probably wondering what all those *s were for. Well, let's go through this line by line.
| char variable = 'G'; |
Standard definition and initialization of a character variable.
| char *pvariable; |
Definition of a character pointer. The * is what tells the compiler that this is actually a pointer. Now why wouldn't you just have a pointer variable type? Well, here's why: Different variables are different lengths.
For example: a long takes up 8 bytes, while an int takes up only 4. The pointer must be defined as the same type as the target. Then the program knows the size of the variable.
| pvariable = &variable; |
This puts the address of variable in pvariable.
| cout << *pvariable; |
This ouputs G as opposed to the address of variable. What do those *s and &s do? The & can be read as "the address of". So read pvariable = &variable; as "place the address of variable in pvariable".
The * is called the dereferencing operator. It goes to the pointer, then goes to the address that the pointer points to, before fetching the data. This causes cout << *pvariable; to output G as opposed to a length address. The * went to the addr contained in pvariable and fetched the G.
DO NOT CONFUSE the dereference operator with the * at the definition of pvariable. They are two DIFFERENT things!!!
Ok, now that you know what pointers are, why are they there? Look at this example (note... this covers passing arguments to functions... you should already know this):
|
#include void doubleit( int myNum ); void main(void) { int myNum; myNum = 5; doubleit( myNum ); cout << "In main() mynum is " << myNum; return(0); } void doubleit( int myNum ) { myNum *= 2; cout << "The value of myNum is " << myNum; return; } |
If you were to run this code it would produce the following output:
The value of myNum is 10 In main() mynum is 5!?!
Why wasn't mynum changed? Because it was passed by value. In order to change mynum in main you must pass it by address. Take a look at the same program, but changed slightly:
|
#include <iostream.h> void main(void) { int myNum; myNum = 5; doubleit( &myNum ); cout << "In main() mynum is " << myNum; return(0); } void doubleit( int *myNum ) { *myNum *= 2; cout << "The value of myNum is " << *myNum; return; } |
This program produces the following output:
The value of myNum is 10 In main() mynum is 10
And as you can see, since we pass it by address it changes the value. Now: what does this have to do with pointers? Well when you pass something by address you actually pass a pointer to it. That's why you use the dereference*. This is the main use for pointers. Now we go to dynamic memory allocation and linked lists.