Pointer are a fundamental part of C. If you cannot use pointers
properly then you have basically lost all the power and flexibility that C
allows. The secret to C is in its use of pointers.
C uses pointers a lot. Why?:
·
It is the only way to express
some computations.
·
It produces compact and
efficient code.
·
It provides a very powerful
tool.
C uses pointers explicitly with:
·
Arrays,
·
Structures,
·
Functions.
NOTE: Pointers are perhaps
the most difficult part of C to understand. C's implementation is slightly
different DIFFERENT from other languages.
A pointer is a variable which contains the address in
memory of another variable. We can have a pointer to any variable type.
The unary or monadic operator &
gives the “address of a variable”.
The indirection or dereference operator *
gives the “contents of an object pointed
to by a pointer”.
To declare a pointer to a variable do:
int *pointer;
NOTE: We must associate a
pointer to a particular type: You can't assign the address of a short int
to a long int, for instance.
Consider the effect of the following code:
int x = 1, y = 2;
int *ip;
ip = &x;
y = *ip;
x = ip;
*ip = 3;
It is worth considering what is going on at the machine
level in memory to fully understand how pointer work. Consider the
following figure. Assume for the sake of this discussion that variable x resides at memory location 100, y at 200
and ip at 1000. Note A pointer is a variable and thus its values need to
be stored somewhere. It is the nature of the pointers value that is new.
Fig. Pointer,
Variables and Memory Now the assignments x = 1 and y = 2
obviously load these values into the variables. ip is declared to be a
pointer to an integer and is assigned to the address of x (&x).
So ip gets loaded with the value 100.
Next y
gets assigned to the contents of ip. In this example ip
currently points to memory location 100 -- the location of x.
So y gets assigned to the values of x -- which is 1.
We have
already seen that C is not too fussy about assigning values of different type.
Thus it is perfectly legal (although not all that common) to assign the
current value of ip to x. The value of ip at this
instant is 100.
Finally
we can assign a value to the contents of a pointer (*ip).
IMPORTANT: When a pointer
is declared it does not point anywhere. You must set it to point somewhere
before you use it.
So ...
int *ip;
*ip = 100;
will
generate an error (program crash!!).
The
correct use is:
int *ip;
int x;
ip = &x;
*ip = 100;
We can
do integer arithmetic on a pointer:
float *flp, *flq;
*flp = *flp + 10;
++*flp;
(*flp)++;
flq = flp;
NOTE: A pointer to
any variable type is an address in memory -- which is an integer address. A
pointer is definitely NOT an integer.
The
reason we associate a pointer to a data type is so that it knows how many bytes
the data is stored in. When we increment a pointer we increase the pointer by
one ``block'' memory.
So for a
character pointer ++ch_ptr adds 1 byte to the address.
For an
integer or float ++ip or ++flp adds 4 bytes to the address.
Consider
a float variable (fl) and a pointer to a float (flp) as shown in followingFig.
Fig. Pointer
Arithmetic
Assume that flp points to fl then if we increment the pointer
( ++flp) it moves to the position shown 4 bytes on. If on the other
hand we added 2 to the pointer then it moves 2 float positions i.e
8 bytes as shown in the Figure.