CS-220 Spring 2018 Lab 5 Arrays, Pointers, and Arrays of Pointers

In C, as we have learned, there is a very close relationship between a pointer to a specific type, and an array of that type - especially a one dimensional array. Today's lab is intended to demonstrate and clarify that relationship, concentrating on when we can use an abstract view of that relationship, and where differences between pointers and arrays can affect the way we code.

I have provided the start of some code to investigate arrays and pointers. One of the things I have provided is declarations of floating point variables, both as scalars, vectors, matrices, pointers, and arrays of pointers. I have also provided tools to print out the format of these variables, as well code to loop through each element of the two dimensional arrays. Your job is to work on this code to help find the answers to the questions in Lab05_report.txt. You don't need to submit any code this week, just edit Lab05_report.txt to add your answers, and turn in the result.

Getting Started

Download and unzip lab05.zip. In the resulting lab05 directory, you will find several files:

Formatting Functions

I have provided a library of formatting functions that show how variables are laid out in memory in C. All of these functions return a char** pointer. Similar to argv, the returned char** pointer is really a vector of strings, where each string contains a line of the formatted description. By convention, each of these lists ends with an empty string. The printFormat function writes first the description of the formatted data (from the first argument, descr), followed by all the lines returned by one of the format function to standard output. These functions are designed to be invoked as:


  printFormat("Formatted version of x",formatXXX(x,...));

... where formatXXX is one of the formatting functions described below.

One thing we haven't studied yet (but will be studying soon) in the formatting functions is the use of the standard library malloc and free functions. I call malloc to get some memory that I can use for my values. I can use that memory until I invoke the free function, which gives it back. In the functions above, each formatXXX function malloc's space for the descriptions, but the printFormat function free's that space, assuming you won't need it again after you have printed it. You shouldn't need to change anything, but I figured I'd mention this so you have a preview of what is coming.

Modifying Main in showArray.c

My recommendations for attacking this lab are as follows:

  1. Look through the declarations and initializations of the various variables in showArray.c. Do things make sense?
  2. Add code to print out at least one of each type of variable in the declarations, and run the code. I've already included code to print out the f0 variable just to give you an example. See if you can figure out which format functions to print out vec1 or vecPtr1 or arr2d, or arrPtrs1d, or even arrPtrsPtrs1d. Hopefully, you will start to get a better feel for how things work by looking at this stuff.
  3. Now, read through the questions in Lab05_report.txt. How can you write code to help you answer those questions? For instance, can you compare values in the arr2d array with values in the arrPtrs1d array using square bracket notation for both? Can you compare the address of the current element of an array with the address of the previous element? (Hint: I've coded the loops to go through the array in row major order.)
  4. Modify and run showArray until you can answer all the questions in Lab05_report.txt.

Submitting your Results

Edit the Lab05_report.txt file from lab05.zip. There are five quick questions to answer in that file. Add your answers to the file, and submit the result on MyCourse under the Lab 05 Submission area.