Skip to main content

11.1) Pointers


A pointer variable holds a memory address for a value of a given type, or the memory address for the first value in a sequence of values of a given type.

Pointer definitions are distinguished from regular variable definitions by use of *:

float* p;
float *q, *r;

Here, p is a pointer to a float value, rather than a variable capable of holding a float value. The positioning of the asterisk suggests that the type of p is 'pointer-to-float'. It is also possible, although arguably less clear, to prefix the pointer variable's name with the asterisk; in fact, this alternative syntax is necessary when attempting to define multiple pointers with a single statement, as for q and r above.

We can do arithmetic with pointer variables to move them forward or backward in memory; if p points to the first value in a sequence of float values, then ++p makes p point to the second value from the sequence, whereas p+1 references the second value without changing p.

A pointer variable can be dereferenced using * to access the value it is pointing at; if p is pointing at a float value, then *p will be that value.