P O I N T E R     V A R I A B L E S

INTRO:
Pointers are remarkably similar to reference variables. In fact reference variables are the same thing, just easier to use, but less dynamic. If you haven't read my little ditty on references variables or if you don't know what they are then click here.

PART 2: What Are They?
Pointers are data types that store memory addresses. In this document we simply use pointers to point to the addresses of variables (the simplest usage of pointers).

PART 2: Declaration
Declaring a pointer is simple, and unlike reference variables you don't have to initialize it as pointing to something (pointers and references do the same thing but you will note that I say pointers point and references refer). In declaring, you must write the type of the variable the pointer will be pointing to, put an asterisk after the type, and make up a name:

int *ptr;


PART 3: How Do They Work
Pointers simply store memory addresses. So to point a pointer to a variable you have to pass it an address; specifically that of the variable you want the pointer to point to (read this sentence five times fast and slur your p's). To have a variable give its address in memory you must use the ampersand (&) operator. Yes, we use this same symbol for creating reference variables, but you can read the differences by clicking here.

For example let's say we create an integer variable called x:

int x;

And we give x a value of 928:

x = 928;

Well now if we put just x somewhere it would be like writing the value 928 since that's what x's value is. But we can't give a pointer 928 because it would interpret that as the memory address you want it to point to (which is most likely NOT where x is located in memory). So we put the ampersand in front of x (&x) and pass that to the pointer variable:

int *ptr;       // declare pointer variable of type integer
int x;          // declare 'x' as an integer

x = 928;        // put value of '928' into 'x'
ptr = &x;       // give 'ptr' the memory address of 'x'
  
If you want to see the difference between putting the ampersand in front and not you can try the following code:


/* -example-source-1------------------------------------------------- */

#include <iostream.h>

void main()
{ 
  int x;            // create an integer variable called 'x'
  x = 10;           // put value of '10' into variable 'x'

  cout << "the value of 'x' is " << x << endl
       << "the memory address of 'x' is " << &x << endl;
}

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

When passing the address of the variable, you don't want to put the asterisk in front of the pointer:

WRONG: *ptr = &x;
RIGHT: ptr = &x;

Okay, this sounds kinda skippy but by this point you should be able to do the following things no sweat (if not go to the bottom of the page to find out how to contact me):
APPENDIX I: Different Uses of the Ampersand (&)
When you use the ampersand in the declaration of a variable it is for creating reference variables. Anywhere else you use it, it is interpreted as "getting the memory address of". Like &x would be intrepreted as "getting the memory address of x". But in declaration: int &x, it is seen as "create a reference variable named". So this (int &x) is read by the compiler as "create a reference variable named x".

Click here to go back to Neil's C++ Stuff.