Lab 2: Using Pointers

In C programs we use pointers (among other things) to 1. Implement reference parameters, 2. To make programs more efficient, 3. To connect data structures, and 3. As basic building blocks for string functions.
Last week we got an introduction to pointers. This week we will read a different description of pointers, and in lab we will focus on experimenting with using pointers. To answer this week's lab questions you will have to experiment by writing code. You may not be able to find the answer to the lab questions just by looking at the reading.

Reading

Read the items listed below to prepare for this week's lab. The reading will be the basis for a short quiz at the beginning of your lab. As you read, remember to write down the main points in an outline (about a written half-page total, or 260+ words), which you will use with a partner in lab to help you create a tutorial. You will turn in this outline by the end of Monday, at midnight, using the Reading Outline form. I suggest you read the lab concepts / questions listed below to help focus your thinking and outline creation. In the outline you will need:

  1. A proposed catchy title and a brief overview of how today's topic is useful

  2. The main points are listed and explained

  3. Source code (plus input/output) illustrates the main points

  4. Explanations accompany the source code

  5. A list of 4+ resources (besides the ones already given on the course web site).

This week read chapters 1 and 2 (pp. 1-13) of A Tutorial on Pointers and Arrays in C by Ted Jensen (also available in the Resources tab of Piazza). You may need to run some of the code and experiment with it to understand it fully.

While you don't have to, for reference you may want to look at the Pointer Sizes and Types, Pointer Operators, and Common Uses of Pointers sections (pp. 15-31) of the book Understanding and Using C Pointers by Richard Reese. As explained last lab, this book is available for free through UIC's Safari, as described on the CS 211 Resources page.

Other

See Nick Parlante's excellent Pointers and Memory tutorial

Concepts / Questions to Address in your Tutorial

Remember that your audience for this tutorial is your classmates at this point in the semester. Imagine a student misses this week's reading, discussion, and lab. What is important for them to know? How might you best explain it to them, using original sample code, corresponding input / output, and insightful explanations? The bulk of this week's tutorial should be sample code and accompanying explanations.

  1. Demonstrate how you can declare an array, and then instead of using [] to access the first array element you can alternatively use a pointer to access the first array element.

  2. Show how rather than use an index to access an array element, you can use a pointer and some arithmetic to access an array element.

  3. Show how you could pass an array to a function and catch it and use it as an array. Similarly again pass the array to a different function, but this time catch and use it as a pointer.

  4. Assume you have an array of integers, and an array of char. Given a pointer to one of the int array elements, show how we can use the ++ operator on the pointer to advance to the next array element. Then show the same thing using a pointer to one of the char array elements. Provide an explanation for why the ++ operator works in both cases, even though the underlying types and sizes are different.