Pointers, Pointers and More Pointers


TOPICS:

  1. What IS a pointer, anyway ?
  2. What's a pointer to another pointer ?
  3. Can't a pointer point to anything ?
  4. Give me the Wrap-Up .
  5. Liz, Where's the straight Text Version ?


What IS a pointer, anyway ?

A pointer is a variable used to store a location.
So, what is a location and why do we care?

A computer has to have memory. The basic unit of memory is the byte.

That memory also has to be organized in some way so that we can access parts of it. It happens that the obvious way to organize it is linearly. That is to say that the memory "starts" at a certain value (say, 1024) and ends at another (say, 640000).

What this means is that the computer has bytes of memory that it can read from and write to running from 10000 to 640000. The starting and end points don't matter -- it could be from 0 to 1000000, or from 0 to 10, then 15 to 30, and so on.

When we write a program that has something like this:

We don't care about memory. Somewhere, 4 bytes are being allocated for each of those integers (4 bytes on a 32bit machine) and numbers are being placed in that allocated memory. No fuss, no muss, no worries about memory.

But just because we aren't dealing with it doesn't mean it isn't happening. Memory is being allocated for all three ints. That memory is being referenced whenever we use those variables.

Remember: Z is not being assigned to x+y. Z, Y and X only exist in the abstract, they do not actually exist, as themselves, anywhere. Instead, what happens is, the compiler lets you author your program in the abstract.

What is really happening is different. The contents of the memory location referred to by X is being added to the contents of the memory location referred to by Y and this result is COPIED INTO the memory location referred to by Z.

YOU DO NOT NEED TO THINK ABOUT THIS.
IT DOESN'T CHANGE THE WAY YOU WORK.

It is only meaningful when you are trying to understand pointers.

So what is a pointer?

A pointer holds a memory location, called an address.

The following appears to make things as confusing as possible, but once understood, the distinction between a pointer and, say, an integer becomes clear:

pointer vs integer

Back to the Topic Index


What's a pointer to another pointer ?

Understanding pointers to other pointers
========================================

Boy, that last one is pretty recursive and hard to follow, Yet if we just remember what we're looking for, it becomes comprehensible (though not easy). When looking at a pointer expression, remember that each dereference operator (*) means "the thing pointed to by."

Given that, we should be able to understand the following.
What gets printed?

example ****s

The answer is "32."

What the heck does "***r = 32" mean?
Let's walk through it.

"int ***r" should be read as:
"r is a (pointer to a) (pointer to a) (pointer to an) int."
Thus, we know that r points to the thing pointed to by q which is the thing pointed to by p, which is x.

When we write:

We are saying:

That's pretty bad. It's hard to follow, but when we break it into clauses and use the magic words ("the thing pointed to" for the "*" in an expression) we cannot go wrong.

The output statement is:

Magic Words

Back to the Topic Index


Can't a pointer point to anything ?

A little more on pointers:

A pointer lets us explicitly refer to ANY memory location. Take the above. Although X refers to a memory location, X referes ONLY to that memory location. You cannot change WHERE X refers, you can only change the value of the memory to which X refers, not X itself.

Moreover, NO OTHER VARIABLE EXCEPT A POINTER can refer to the memory location "occupied" by X. X, and only X, may refer to that location.

One difference between a pointer variable and any other variable is that a pointer variable may be used with the * operator.

Back to the Topic Index


Give me the Wrap-Up.

The Wrapup:

You can change the contents of an integer. You can change the contents of a pointer_to_an_integer.

Back to the Topic Index


Back to the Top of this Page


Back to the Top of this page


[Back to the Index]

graphics images Copyright 1998-2003 Elizabeth Fraley HTML code copyright 1998-2003 Elizabeth Fraley. Permission is given to provide these pages in their original, unaltered form online for educational purposes. They may not be republished or included on a CDROM, disk or other media or with any published work without the express permission of the copyright holder (this includes FAQ books).