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.
Download and unzip lab05.zip. In the resulting lab05 directory, you will find several files:
- showArray.c - This is the C code that contains the main function for today's lab, and the file you will need to edit to finish the lab. The main function in this file declares and initializes all the variables, and then has some comments and some starter code to suggest ways to answer the questions in today's lab.
- format.h A file that contains the prototypes for several different functions to format different kinds of data. Look below in these instructions for details on these functions.
- format.c The definitions of the formatting functions. Feel free to read through these -- almost all of what is in this file is stuff we've been studying -- but you shouldn't need to modify any of this code.
- Makefile A standard make file to build and run the showArray program. Targets include test and clean.
- Lab05_report.txt The list of questions to answer for this lab. Edit this list, and submit your answers to these questions on myCourses.
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.
- formatPointer Takes a pointer as an argument, and formats the value of that pointer inside a "box".
- formatScaler Takes a pointer to a float as an argument, and formats the pointer using formatPointer, and adds an arrow and the value pointed at to the formatted result.
- formatVector Takes a pointer to a list of floats as the first argument, and the number of floats in that list as a second argument. The formatted result contains the formatted pointer, followed by an arrow, followed by a list of values.
- formatMatrix Takes a pointer to a list of floats as the first argument, and the number of rows and columns in the matrix as second and third arguments. Note that if the matrix is declared as a two dimensional matrix, you will need to cast it to a float pointer when passing it into this function to avoid compiler warning messages. The formatted result contains the formatted pointer, followed by an arrow, followed by formatted matrix values, one line per row (assuming the pointer parameter points to values in row major order.)
- formatPointerList Assumes the first argument is a pointer to an array of pointers to scalar floating point values. The second argument defines how many pointers are in this array. The result will show the initial pointer pointing to the list of pointers, and each pointer in the list pointing to it's floating point value.
- formatPointerVectorList Assumes the first argument is a pointer to an array of pointers to floating point vectors. The second argument tells how many pointers are in the array of pointers. The second argument tells how many floating point values are in each vector. The result contains the argument pointer, pointing to a list of pointers, where each pointer points to a list of floating point values.
- formatPointerPointerList Assumes the first argument is a pointer to a list of pointer, and that each item in that list is a secondary list of pointers, and that each item in the secondary list points to a scale floating point value. The second argument tells how many pointers are in the primary lists of pointers. The third argument tells how many pointer are in each secondary list of pointers. The result shows three levels of pointers, followed by the floating point value ultimately pointed at.
- printFormat The printFormat function prints out the descriptions returned by one of the formatXXX functions described above.
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:
- Look through the declarations and initializations of the various variables in showArray.c. Do things make sense?
- 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.
- 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.)
- 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.