ProgrammerGuide.Net | Step by Step Programmer Guide



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 (see Figure 5.3): Diagram showing an 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. Figure 5.3. 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:
int ar[5], *ip;
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 now are shown in Figure 5.4 (?? means uninitialized). Diagram showing 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]’ Figure 5.4. 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].

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:
int i;
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’. Looking at Table 2.9, 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.
#include <stdio.h>
#include <stdlib.h>
int x, *p;
p = &x; /* initialise pointer */
*p = 0; /* set x to zero */
printf(“x is %d\n”, x);
printf(“*p is %d\n”, *p);
*p += 1; /* increment what p points to */
printf(“x is %d\n”, x);
(*p)++; /* increment what p points to */
printf(“x is %d\n”, x);

Please click here for related products on Amazon!


Add comment

Want to Check Celebrities News?