CSC161 2010F Imperative Problem Solving

Lab: Pointers

Summary: In this lab, you will explore some of the oddities and uses of pointers.



a. Create a new directory for this laboratory.

b. Make a copy of pointers.c.


Exercise 1: Where Are They?

As you may recall, the & operation gives you the address of a variable. Let's look at the addresses of a few variables in our program.

a. Augment pointers.c to print the addresses of monkey, emu, and zebra.

The %p pattern is used for pointers (which should be cast to type (void *). Pointers print in hexadecmial form, so you may also want to print it as an integer. For example, we might print out the address of monkey.

  printf ("&%s: %p (%u)\n", "monkey", (void *) &monkey, (unsigned int) &monkey);

b. What do you discover about the relative addresses of the three variables?

c. What do you expect to have happen if you run the program multiple times? What does this suggest to you? (Ask your class mentor or professor if it doesn't make sense.)

d. Rearrange the declarations of monkey, emu, and zebra and see what effect this has. Then restore their order.

e. Add code to determine where in memory bison and yak are stored. Where are they relative to the other values?

Exercise 2: Where Are They? Revisited

You may note that we've also declared an integer parameter and variable in the recursive gnu function. Let's find out where in memory in each recursive call.

a. Extend gnu to print the address of ibus, walrus, and seal at the start of each call. (That is, right after the declaration of seal.)

b. What did you learn about what happens with parameters and variables during recursive calls? Be prepared to discuss your answer with the class.

c. As you may recall, we can declare local variables as static. Change the declaration of walrus to

  static int walrus;

d. What effect do you have this to have on the location of walrus in recursive calls? (You may want to look up the static modifier in our textbook.)

e. Check your answer experimentally.

f. Delete the static modifier.

Exercise 3: Where Are They, Revisited?

What happens when we have array variables? Let's see.

a. Refresh your memory on the relationship between the address of monkey and the address of emu.

b. Add a declaration for anthill, a size 10 array of integers between the declaration of monkey and the declaration of emu.

c. What effect do you expect this array to have on the relationship between the address of monkey and the address of emu?

d. Check your answer experimentally.

e. Refresh your memory on the relationship between the address of walrus and the address of seal.

f. Add a declaration for lionpride, a size ibus array of integers between the declaration of walrus and the declaration of seal.

g. What effect do you expect this array to have on the relationship between the address of walrus and the address of seal?

h. Check your answer experimentally.

Exercise 4: Undeclared Variables

a. What is the initial value of wildcat?

b. Does that value every change (e.g., if you run the program multiple times or change the code of the program)?

c. What happens if you try to assign to the thing that wildcat points to?

Exercise 5: Pointing to Locals

a. Add the following lines to your main.

  printf("*giraffe: %d\n", *giraffe);
  printf("*giraffe: %d\n", *giraffe);

b. What do you expect the output of this code to be?

c. Run the code to confirm your analysis.

d. Explain the output.

Exercise 6: Swapping Values

Write a unit test to verify that the swap function given in the reading works as advertised. You should make sure to see what happens when you

Exercise 7: Mungeing Values

Write a procedure, munge, that replaces its double parameter with 1 minus the square of that parameter.

For example,

  d = 0.5;
  for (i = 0; i < 5; i++)
      munge (&d);
      printf ("%lf\n", d);

Should give




Friday, 21 February 2003 [Samuel A. Rebelsky]

  • Created for CSC 195 2003S.

Monday, 25 October 2010 [Samuel A. Rebelsky]

  • Updated for a new class.
  • Dropped problems that use malloc. (We're not dealing with dynamic allocation yet.)
  • Added some more Where in memory is this variable? questions.
  • Added the swap and munge exercises.
  • Some reformatting.

Wednesday, 27 October 2010 [Samuel A. Rebelsky]

  • A few minor updates.


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Oct 27 09:46:20 2010.
The source to the document was last modified on Wed Oct 27 09:46:18 2010.
This document may be found at

Samuel A. Rebelsky,