Review of Pointers
Pointers are a powerful programming tool which are often
misused and misunderstood. In the simplest terms, a pointer
is the address of an object or basic data type. A pointer variable
is a variable that contains the address of another variable, be it an object
or basic data type
(we say it "points to" the object). We assume that this is not your
first exposure to pointers.
An important aid to understanding pointers is the use of diagrams.
Diagrams such as these are also vital when debugging programs which
use pointers.
Basic Diagrams
- A simple variable is drawn with a box that is labelled with the variables
name and type. The drawing of a simple integer variable named age
is shown below.
- More complex variables use more complex boxes. Typically arrays/vectors are
drawn horizontally while structs and objects are drawn vertically.
Given the declarations
struct Astruct
{
int y;
char c;
};
int intArray[ 4 ];
Astruct myStruct;
we would draw diagrams like these
- A pointer variable is drawn as a labelled box like any simple variable.
The value of a pointer variable is the address of the thing to
which it points.
However, rather than containing the actual address of the variable to which it
points, the box contains the end of an arrow that points to the variable.
If the pointer variable doesn't point to any entity, its value is 0 and is
drawn as a box with a slash from upper right to lower left.
A pointer variable recieves a value (points to something) through assignment.
It's assigned the address of an existing variable or a new
created variable.
The & operator
The & (ampersand) operator is used to determine the address of an variable.
That address can then be assigned to a pointer variable. Pictorially,
we are creating an arrow between the box representing the pointer variable and
the entity to which it points. The code below results in the picture below.
int value;
int *pValue;
int value = 17;
pValue = &value;
Pointer assignment
The value of a pointer variable may be assigned to another pointer variable
of the same type. Suppose that thing1 and thing2 are
both "pointers to int" and thing1 points to an integer whose
value is 17 as represented in this diagram
What is the effect of the statement thing2 = thing1; assigning
the value of thing1 to the variable thing2?
Pointer dereferencing
Consider the following diagram in which thing1 is once again
a "pointer to int" pointing to an integer whose value is 17.
The * (star) operator is used to "dereference" a pointer.
The expression *thing1
means "the integer at which thing1 points". In terms of our picture,
it means "follow the arrow". We can use *thing1 where ever an integer
can be used.
One common example is to use dereferencing to change the variable to which
the pointer points. The statement *thing1 = 42; changes the diagram
above to
Note that the precedence of the * operator is lower than the dot operator
and the arrow operator. You must be sure to use parenthesis when necessary
to get the proper precedence. Consider the incomplete Car class definition
and code fragment below
class Car
{
public:
Car( );
void Start( );
// other public methods
private:
// private data
};
Car chevy;
Car *carPtr = & chevy;
// to Start the car using the pointer we can write
carPtr->Start( );
// or this (although the arrow syntax above is much preferred)
(*carPtr).Start( );
// but NOT this code which is a compiler error
*carPtr.Start( );
Pointers, Arrays and Arithmetic
One of the confusing aspects of using pointers is their strong
relationship with arrays. The name of an array is defined to
be a pointer to the first (0-th) element of the array.
Unlike a pointer, the name of an array cannot be changed to point anywhere
other than the 0-th element of the array. Otherwise, the name of the
array looks and acts like a pointer.
Given the declaration int bongo[ 4 ];, the name
bongo can be used as a pointer to reference bongo[ 0 ];.
Using pointer dereferencing, the statement *bongo = 42; is
equivalent to bongo[ 0 ] = 42;.
If a pointer value references a particular array element, then adding an
integer, N, to that pointer value results in a new pointer that references
the array element N elements away from the original element.
This idea is captured in the diagram below.
Combining these two ideas together we observe that the Nth element of
an array called myArray can be referenced as myArray + N.
Thus the expressions myArray[ N ] and *(myArray + N)
are equivalent and both represent the contents of the Nth element of the array.
Also, the expressions &myArray[ N ] and
myArray + N are equivalent and both represent the address of (pointer to)
the Nth element of the array.
Last Modified: Monday, 28-Aug-2006 10:16:04 EDT