CSC161 2010F Imperative Problem Solving

Laboratory: Pointers, Revisited

Summary: We continue to explore pointers, particularly the relationship between pointers and arrays.

Prerequisites: Familiarity with pointers.

Preparation

Finish the lab on pointers.

Exercises

Exercise 1: Addresses of Arrays

In exercise 3 of the lab on pointers, you added code to declare two arrays.

a. Add code to determine the address of element 0 of each of those arrays.

b. Add code to determine the address of element 1 of each of those arrays.

c. Add code to determine the address of element 2 of each of those arrays.

d. What do these results suggest about the organization of the arrays?

e. Add the following line to your program.

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

f. What do you expect this to print out (relative to what you printed for element 0)?

g. Check your answer experimentally.

Exercise 2: Pointers and Arrays

a. Add the following lines to your main method after the declaration of anthill.

  int *wombat = anthill;

b. What value do you expect to get when you print out wombat? (Express this relative to the value of anthill[0].)

c. Check your answer experimentally.

d. What value do you expect to get when you print out wombat + 1? (Express this relative to the value of anthill[0].)

e. Check your answer experimentally.

f. What does this suggest about adding to pointers?

Exercise 3: Arrays and Pointers, Revisited

a. Add the following code to initialize anthill.

  int i;
  for (i = 0; i < 9; i++)
    anthill[i] = i;
  anthill[9] = -1;

b. What do you expect the following code to do?

  int *wombat = anthill;
  while (*wombat >= 0)
    {
      printf ("%d\n", *wombat);
      wombat++;
    }

c. Check your answer experimentally.

 

History

Wednesday, 27 October 2010 [Samuel A. Rebelsky]

  • Created.

 

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:41:47 2010.
The source to the document was last modified on Wed Oct 27 09:41:45 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/pointers-revisited-lab.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/pointers-revisited-lab

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2010 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.