User Tools

Site Tools


cs270:pointers

Pointer Variables

For this exercise, you'll want to demonstrate the output for each part to get full credit. If completed during class, demonstrate to the instructor. Otherwise, save and email screenshots of each one (or just one if all runs can be displayed all at once).

  1. Download the file ptr-examples.c
  2. Compile and run the program
  3. Make the following changes:
    • Add a width specifier in the appropriate printf statements to make the addresses line up (like if they were in a table), right-justified.
    • If you don't remember how to use width specifiers, look at the printf section in Dive Into Systems.
    • Comment out the call to free and recompile. Does the program still work?
  4. Now download the file ptr-errors.c
  5. Compile and run the program, even if there are warnings. What goes wrong?
  6. Edit the file and fix the problem by initializing the pointer variable p as in ptr-examples.c
  7. Compile and run again. What is odd about the output now?
  8. Will the program work properly if you comment out the call to free? Why or why not? Try it and see what happens!

One thing to learn from the exercise is this: using the -Wall option to gcc can save us some grief by flagging uninitialized pointer variables for us. ALWAYS USE -Wall!

cs270/pointers.txt · Last modified: 2020/09/01 12:04 by scarl