Pointing the Way
So now that we know what pointers are, how do we create and use them?
Remember, a pointer is a variable just like any other.
So, in C, we must declare our pointer variables.
A pointer to a character would be declared like:
char *p;
which means to define a variable called
p
which will normally
hold the address of some character.
The syntax is meant to indicate the the expression
*p
should be
a character.
In fact, that identifies the first aspect of using pointers.
The
*
operator
dereferences
a pointer, which means that
the value of the expression
*p
is the character to which
p
points.
For example, the code fragment
*p = 'b';
printf("%c\n", *p);
would set the character to which
p
points to be the character
'b'
and would then print it out.
This illustrates that a dereferenced pointer may be used anywhere any
other variable or expression of that type may be used.