It is just as easy to forget to initialize a pointer variable as it is to
forget to initialize a regular variable. Unfortunately, the consequences of
doing so can often be confusing. So let's take a look at an example.
Take a look at the program ``mistake1.c'' in your ``examples''
directory (or view it directly). Notice that we use the pointer ``p'' in two
ways. First, we try to obtain the value of the variable it is
``pointing to'', and then we try to store to the variable it is
``pointing to.''
Where is it actually pointing?
Click here for the answer
Run the program. Can you explain what happens?
Click here for the answer
As you begin to write programs that use pointers, get used to that message. It
often means that you are using a pointer variable that is uninitialized or
contains some other bad value. And mistakes like that are hard to find in
large programs, since the error message gives you no clue as to what part of
the program was executing when the error occurred.
Hamlet Project
hamlet@cs.utah.edu