|

Every
variable has an address. Even without knowing the
specific address of a given variable, you can store
that address in a pointer.
For
example, suppose that howOld is an integer. To declare
a pointer called pAge to hold its address, you would
write
int
*pAge = 0;
This
declares pAge to be a pointer to int. That is, pAge
is declared to hold the address of an int.
Note
that pAge is a variable like any of the variables.
When you declare an integer variable (type int),
it is set up to hold an integer. When you declare
a pointer variable like pAge, it is set up to hold
an address. pAge is just a different type of variable.
In
this example, pAge is initialized to zero. A pointer
whose value is zero is called a null pointer. All
pointers, when they are created, should be initialized
to something. If you don’t know what you want to
assign to the pointer, assign 0. A pointer that
is not initialized is called a wild pointer. Wild
pointers are very dangerous.
NOTE: Practice safe computing: Initialize
your pointers!
If
you do initialize the pointer to 0, you must specifically
assign the address of howOld to pAge. Here’s an
example that shows how to do that:
unsigned
short int howOld = 50; // make a variable
unsigned
short int * pAge = 0; // make a pointer
pAge
= &howOld; // put howOld’s address in pAge
The
first line creates a variable—howOld, whose type
is unsigned short int—and initializes it with the
value 50. The second line declares pAge to be a
pointer to type unsigned short int and initializes
it to zero. You know that pAge is a pointer because
of the asterisk (*) after the variable type and
before the variable name.
The
third and final line assigns the address of howOld
to the pointer pAge. You can tell that the address
of howOld is being assigned because of the address
of operator (&). If the address of operator
had not been used, the value of howOld would have
been assigned. That might, or might not, have been
a valid address.
At
this point, pAge has as its value the address of
howOld. howOld, in turn, has the value 50. You could
have accomplished this with one less step, as in
unsigned
short int howOld = 50; // make a variable
unsigned
short int * pAge = &howOld; // make pointer
to howOld
pAge
is a pointer that now contains the address of the
howOld variable. Using pAge, you can actually determine
the value of howOld, which in this case is 50. Accessing
howOld by using the pointer pAge is called indirection
because you are indirectly accessing howOld by means
of pAge. Later in this unit you will see how to
use indirection to access a variable’s value.
New Term: Indirection means accessing
the value at the address held by a pointer. The
pointer provides an indirect way to get the value
held at that address.
Pointer
Names
Pointers
can have any name that is legal for other variables.
This book follows the convention of naming all pointers
with an initial p, as in pAge or pNumber.
The
Indirection Operator
The
indirection operator (*) is also called the dereference
operator. When a pointer is dereferenced, the value
at the address stored by the pointer is retrieved.
Normal
variables provide direct access to their own values.
If you create a new variable of type unsigned short
int called yourAge, and you want to assign the value
in howOld to that new variable, you would write
unsigned
short int yourAge;
yourAge
= howOld;
A
pointer provides indirect access to the value of
the variable whose address it stores. To assign
the value in howOld to the new variable yourAge
by way of the pointer pAge, you would write
unsigned
short int yourAge;
yourAge
= *pAge;
The
indirection operator (*) in front of the variable
pAge means "the value stored at." This assignment
says, "Take the value stored at the address in pAge
and assign it to yourAge."
NOTE: The indirection operator (*)
is used in two distinct ways with pointers: declaration
and dereference. When a pointer is declared, the
star indicates that it is a pointer, not a normal
variable. For example,
unsigned
short * pAge = 0; // make a pointer to an unsigned
short
When
the pointer is dereferenced, the indirection operator
indicates that the value at the memory location
stored in the pointer is to be accessed, rather
than the address itself.
*pAge
= 5; // assign 5 to the value at pAge
Also
note that this same character (*) is used as the
multiplication operator. The compiler knows which
operator to call, based on context.
|