CSC161 2010F Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
-
[Assignment]
[Lab]
Groupings:
[EBoards]
[Assignments]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Summary: In this lab, you will explore some of the oddities and uses of pointers.
Contents:
a. Create a new directory for this laboratory.
b. Make a copy of pointers.c.
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?
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.
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.
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?
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.
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
main).
main) with
a global variable.
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
Friday, 21 February 2003 [Samuel A. Rebelsky]
Monday, 25 October 2010 [Samuel A. Rebelsky]
malloc. (We're not dealing
with dynamic allocation yet.)
Where in memory is this variable?questions.
Wednesday, 27 October 2010 [Samuel A. Rebelsky]
[Skip to Body]
Primary:
[Front Door]
[Schedule]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
-
[Assignment]
[Lab]
Groupings:
[EBoards]
[Assignments]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
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 http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/pointers-lab.html.