# Lab: Pointers

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

Contents:

## Preparation

a. Create a new directory for this laboratory.

b. Make a copy of `pointers.c`.

## Exercises

### 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.)

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`?

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`?

### 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`.

```  lemur();
printf("*giraffe: %d\n", *giraffe);
otter();
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

• Swap two global variables.
• Swap two local variables (e.g., both declared in `main`).
• Swap a variable with itself.
• Swap two array elements.
• Swap a local variable (e.g., declared in `main`) with a global variable.

### 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

```0.750000
0.437500
0.808594
0.346176
0.880162
```

## History

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]

This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/pointers-lab.html`.