Address (or "pointer") Variables

For every simple data type (int, char, etc.) you can create a variable that holds the address of such a data type.

int i;        // can hold an integer value

int *p;       // can hold the address of an int variable

Note this syntax carefully. The * denotes a new data type, one that can hold an address.

int *p is read as "p is a pointer to an int"

Suppose that i is stored at address 200, and i's value is 4. Suppose further that p does hold the address of i. Then p's value is 200.

How can we get the address of a variable? There is a special operator that will get it: &

Suppose we have the following declarations:

int i;     // can hold an int
int *p;    // can hold the address of an int variable

i = 4;     // store the value 4 in variable i
p = &i;   // find and store the address of i into p.

Now, "p points to i" or "p contains the address of i"

How can we access the value stored in i by using the pointer variable p? We "dereference" the pointer.

The "dereference operator" is the *. Write the * before the pointer and you have an expression that refers to the "value pointed to" by the pointer.

So given the declarations and assignments above, we can:

cout << i;       // 4
cout << *p;      // 4

In the first, we print the value stored in variable i. In the second, we print the value stored in the int variable pointed to by p. They are the same thing.

Passing Addresses to Functions as Arguments

Pointers allow us to share data between parts of a program - i.e. among functions. They will allow us to write functions that do alter values of simple variables in the calling program.

Prototype format: Return_type function_name(data_type *);

Function header format: Return_type function_name(data_type *argument_name)

Calling statement format 1: Variable = function_name(&argument);

or

Calling statement format 2: Funtion_name(&argument);

Pointer Arithmetic

Pointer arithmetic gives us the ability to make a pointer variable point at the next or previous memory address for its type.

So given a pointer to a memory location, you can add or subtract to/from it and make it point to a different place in memory.

The format for pointer arithmetic is just like that of a regular variable.

ptr++;             //will increment ptr to the next memory address

ptr = ptr + 4;     //will increment ptr 4 places from its original address

ptr -= 3;          //will decrement ptr 3 places from its original address

Pointer arithmetic can also be used when assigning values to certain addresses.

*(ptr + 1) = 5;     //assigns the value 5 to the address ptr + 1

*(ptr + x) = 8;     //assigns the value 8 to the address ptr + x where x is an integer variable

This is a useful way to access array elements.

char str[80] = {"beet"};

(str + 1) points to 1st 'e'
(str + 2) points to 2nd 'e'
etc.

So:

*(str + 2) = 'a';

changes "beet" to "beat". It's the same effect as the subscript notation:

str[2] = 'a';

Note: when the compiler sees subscript notation it internally changes it into the *(str + sub) notation. So internally, the compiler is always using pointer notation.