Introduction
Using pointers is a bit like riding a bicycle. Just when you think that you'll
never understand them—suddenly you do! Once learned the trick is hard to forget.
There's no real magic to pointers, and a lot of readers will already be familiar
with their use. The only peculiarity of C is how heavily it relies on the use of
pointers, compared with other languages, and the relatively permissive view of
what you can do with them.
Declaring Pointers
Of course, just like other variables, you have to declare pointers before you can use them. Pointer declarations look much like other declarations: but don't be misled. When pointers are declared, the keyword at the beginning (c int, char and so on) declares the type of variable that the pointer will point to. The pointer itself is not of that type, it is of type pointer to that type. A given pointer only points to one particular type, not to all possible types. Here's the declaration of an array and a pointer:int ar[5], *ip;
We now have an array and a pointer are array with four elements (labelled 'ar[0]' to 'ar[4]') and a pointer called 'ip' which does not currently have any connection to the array. An array and a pointerThe * in front of ip in the declaration shows that it is a pointer, not an ordinary variable. It is of type pointer to int, and can only be used to refer to variables of type int. It's still uninitialized, so to do anything useful with it, it has to be made to point to something. You can't just stick some integer value into it, because integer values have the type int, not pointer to int, which is what we want. (In any case, what would it mean if this fragment were valid:
ip = 6;
What would ip be pointing to? In fact it could be construed to have a number of meanings, but the simple fact is that, in C, that sort of thing is just wrong.)Here is the right way to initialize a pointer:
ip = &ar[3];
In that example, the pointer is made to point to the member
of the array ar whose index is 3, i.e. the fourth member. This
is important. You can assign values to pointers just like
ordinary variables; the difference is simply in what the value
means. The values of the variables that we have shown (?? means
uninitialized). An array with four elements (labelled 'ar[0]' to
'ar[4]') each of which has an undefined value, and a pointer
called 'ip' which contains the address of 'ar[3]'. Array and
initialized pointerYou can see that the variable ip has the
value of the expression &ar[3]. The arrow indicates that, when
used as a pointer, ip points to the variable ar[3].
It's worth remembering that structure tags can safely be used as
ordinary identifiers as well. They only mean something special
when they are preceded by the keyword struct. It is quite common
to see a structured object being defined with the same name as
its structure tag.
What is this new unary &? It is usually described as the
‘address-of’ operator, since on many systems the pointer will
hold the store address of the thing that it points to. If you
understand what addresses are, then you will probably have more
trouble than those who don't: thinking about pointers as if they
were addresses generally leads to grief. What seems a perfectly
reasonable address manipulation on processor X can almost always
be shown to be impossible on manufacturer Y's washing machine
controller which uses 17-bit addressing when it's on the spin
cycle, and reverses the order of odd and even bits when it's out
of bleach. (Admittedly, it's unlikely that anyone could get C to
work an an architecture like that. But you should see some of
the ones it does work on; they aren't much better.)
Applying the & operator to an operand returns a pointer to the
operand:
float f;
/* '&i' would be of type pointer to int */
/* '&f' would be of type pointer to float */
A pointer is only useful if there's some way of getting at the thing that it points to; C uses the unary * operator for this job. If p is of type ‘pointer to something’, then *p refers to the thing that is being pointed to. For example, to access the variable x via the pointer p, this would work:
The precedence of (*p)++ deserves some thought. It will be given more later, but for the moment let's work out what happens. The brackets ensure that the * applies to p, so what we have is ‘post-increment the thing pointed to by p’. It turns out that ++ and * have equal precedence, but they associate right to left; in other words, without the brackets, the implied operation would have been *(p++), whatever that would mean. Later on you'll be more used to it—for the moment, we'll be careful with brackets to show the way that those expressions work.

