Don't Point That Thing at Me

The topic of this lab is universally viewed by experienced C programmers as the hardest thing about programming in C. In fact, I was once giving a series of training seminars to a group of professional C programmers and announced that the next class would be on the "hard stuff." At that point, several people in the class said "Ah, pointers."

However, as we'll see today, there need be no fear or confusion regarding pointers. Conceptually, they are actually quite simple, and if we can keep focused on the important principles, we'll be able to use them effectively.

What are pointers?

The cardinal principle of pointers is this.
A pointer is best thought of as the memory address of the thing to which it points, and, in most implementations, it is.
When we discussed the general organization of a computer and of memory in particular, we noted that memory is divided into words each of which has an address. It's that address that a pointer holds. A pointer is just like any other variable in that it has a value, but a pointer's value tells us where some information is as opposed to what some information is.

Suppose we have a pointer, p, to a character, and let's say that the character it points to is the character 'b'. Then we have a situation as shown in this figure.

[Pointer to a character]

Here both the pointer and the character are stored in memory, but it so happens that the number stored in the memory location we've called p is the memory address of the character to which p points.

Usually, we don't worry about keeping track of the actual memory addresses as we program. The compiler does that for us. Instead, we generally draw pointers and the things they point to by using arrows as shown in this figure.

[Pointer Figure]

Proceed to the next part.