{={copyright-samr}{1}} {={pdf}{pointers-revisited-lab}} Summary: We continue to explore pointers, particularly the relationship between pointers and arrays. Prerequisites: Familiarity with pointers.
Finish the lab on pointers.
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.
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?
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.
Wednesday, 27 October 2010 [Samuel A. Rebelsky] * Created.