home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP8_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
19KB
|
455 lines
Chapter 8 - Pointers
WHAT IS A POINTER?
Simply stated, a pointer is an address. Instead of
being a variable, it is a pointer to a variable stored
somewhere in the address space of the program. It is always
best to use an example so load the file named POINTER.C and
display it on your monitor for an example of a program with
some pointers in it.
For the moment, ignore the data declaration statement
where we define "index" and two other fields beginning with
a star. It is properly called an asterisk, but for reasons
we will see later, let's agree to call it a star. If you
observe the first statement, it should be clear that we
assign the value of 39 to the variable "index". This is no
surprise, we have been doing it for several programs now.
The next statement however, says to assign to "pt1" a
strange looking value, namely the variable "index" with an
ampersand in front of it. In this example, pt1 and pt2 are
pointers, and the variable "index" is a simple variable.
Now we have a problem. We need to learn how to use pointers
in a program, but to do so requires that first we define the
means of using the pointers in the program.
The following two rules will be somewhat confusing to
you at first but we need to state the definitions before we
can use them. Take your time, and the whole thing will
clear up very quickly.
TWO VERY IMPORTANT RULES
The following two rules are very important when using
pointers and must be thoroughly understood.
1. A variable name with an ampersand in front of it defines
the address of the variable and therefore points to the
variable. You can therefore read line seven as "pt1 is
assigned the value of the address of index".
2. A pointer with a "star" in front of it refers to the
value of the variable pointed to by the pointer. Line
ten of the program can be read as "The stored (starred)
value to which the pointer "pt1" points is assigned the
value 13". Now you can see why it is convenient to
think of the asterisk as a star, it sort of sounds like
the word store.
MEMORY AIDS
1. Think of & as an address.
2. Think of * as a star referring to stored.
Page 52
Chapter 8 - Pointers
Assume for the moment that "pt1" and "pt2" are pointers
(we will see how to define them shortly). As pointers, they
do not contain a variable value but an address of a variable
and can be used to point to a variable. Line 7 of the
program assigns the pointer "pt1" to point to the variable
we have already defined as "index" because we have assigned
the address of "index" to "pt1". Since we have a pointer to
"index", we can manipulate the value of "index" by using
either the variable name itself, or the pointer.
Line 10 modifies the value by using the pointer. Since
the pointer "pt1" points to the variable "index", then
putting a star in front of the pointer name refers to the
memory location to which it is pointing. Line 10 therefore
assigns to "index" the value of 13. Anyplace in the program
where it is permissible to use the variable name "index", it
is also permissible to use the name "*pt1" since they are
identical in meaning until the pointer is reassigned to some
other variable.
ANOTHER POINTER
Just to add a little intrigue to the system, we have
another pointer defined in this program, "pt2". Since
"pt2" has not been assigned a value prior to statement 8, it
doesn't point to anything, it contains garbage. Of course,
that is also true of any variable until a value is assigned
to it. Statement 8 assigns "pt2" the same address as "pt1",
so that now "pt2" also points to the variable "index". So
to continue the definition from the last paragraph, anyplace
in the program where it is permissible to use the variable
"index", it is also permissible to use the name "*pt2"
because they are identical in meaning. This fact is
illustrated in the first "printf" statement since this
statement uses the three means of identifying the same
variable to print out the same variable three times.
THERE IS ONLY ONE VARIABLE
Note carefully that, even though it appears that there
are three variables, there is really only one variable. The
two pointers point to the single variable. This is
illustrated in the next statement which assigns the value of
13 to the variable "index", because that is where the
pointer "pt1" is pointing. The next "printf" statement
causes the new value of 13 to be printed out three times.
Keep in mind that there is really only one variable to be
changed, not three.
Page 53
Chapter 8 - Pointers
This is admittedly a very difficult concept, but since
it is used extensively in all but the most trivial C
programs, it is well worth your time to stay with this
material until you understand it thoroughly.
HOW DO YOU DECLARE A POINTER?
Now to keep a promise and tell you how to declare a
pointer. Refer to the third line of the program and you
will see our old familiar way of defining the variable
"index", followed by two more definitions. The second
definition can be read as "the storage location to which
"pt1" points will be an int type variable". Therefore,
"pt1" is a pointer to an int type variable. Likewise, "pt2"
is another pointer to an int type variable.
A pointer must be defined to point to some type of
variable. Following a proper definition, it cannot be used
to point to any other type of variable or it will result in
a "type incompatibility" error. In the same manner that a
"float" type of variable cannot be added to an "int" type
variable, a pointer to a "float" variable cannot be used to
point to an integer variable.
Compile and run this program and observe that there is
only one variable and the single statement in line 10
changes the one variable which is displayed three times.
THE SECOND PROGRAM WITH POINTERS
In these few pages so far on pointers, we have covered
a lot of territory, but it is important territory. We still
have a lot of material to cover so stay in tune as we
continue this important aspect of C. Load the next file
named POINTER2.C and display it on your monitor so we can
continue our study.
In this program we have defined several variables and
two pointers. The first pointer named "there" is a pointer
to a "char" type variable and the second named "pt" points
to an "int" type variable. Notice also that we have defined
two array variables named "strg" and "list". We will use