Pointers

A pointer is a variable holding a memory address. For example, if the variable a contains the address of the variable b, then a is pointing to b.

Declaring a pointer

We have to declare a pointer differently to how we declare a variable. When we declare a variable, we can use

type name;

or similar. When declaring a pointer, we use this form:

type *name;

(note the *)

Type is the base type of the pointer, and also defines the type of variable the pointer can point to.

There are two special pointer operators: * and &.

& returns the memory address of a variable. For example:

a = &b;

puts the memory address of the variable b into the variable a.

* returns the value located at the address of the following operator. For example:

a = *b;

puts the value in memory pointed by b into a. If b contains the memory address of another variable, a will have the value of that variable.