Lab: Arrays as Pointers

Arrays and pointers are closely related in C and often may be used interchangeably.

To get started, download the starter code for this lab: pointers-arrays.tar.gz and run the following terminal commands:

$ cd csc161/labs
$ tar xvzf ~/Downloads/pointers-arrays.tar.gz
$ cd pointers-arrays
$ code .

The .tar.gz file is a compressed archive that contains multiple files, a bit like the .zip files you may have used on other computers. The files in a .tar.gz archive will generally be contained within a directory, which in this case was named pointers-array.

A. Looping through arrays with pointers

Consider the following declaration within a C program:

int notes[8] = {523, 587, 659, 698, 783, 880, 987, 1048};

C uses this declaration in at least two ways:

  • The machine allocates space for 8 int values within main memory.
  • The identifier notes is used to reference the location of the first element in main memory.

To clarify, the variable notes identifies the start of the the array. Thus, notes[0] specifies the first value in the block of memory, notes[1] specifies the second value in the block of memory, etc. This interpretation of the array has two consequences when using functions.

  • The variable notes specifies an address. Array parameters are thus passed by reference, because the address is given. This means that the machine only has to pass the reference to the array—not copy the entire array— when passing arrays to functions.
  • The variable notes specifies a starting address, but not an ending address or array. Programmers must must keep track of size separately to use an array in processing.

Open the max-array.c file you unpacked in the previous step and read through it. Make a prediction of the output and then check your prediction by compiling and running max-array.


Exercises

The exercises for this part will run through a series of changes you’ll need to make to the max-array.c source file.

  1. Make a copy of the find_max function, and rename it to find_max_pointer.

  2. Modify find_max_pointer so it defines parameter array as a double* rather than a double array. Make sure your modified find_max accepts a pointer parameter, not a double pointer or pointer to an array. Update main so it calls find_max_pointer and make sure it produces the same result as the original find_max.

  3. Modify find_max_pointer so it uses pointer operations to traverse the array instead of indexing. Specifically, your find_max_pointer function should not include an expression like this: array[i]. Instead, you should be incrementing a pointer to walk through the array. Again, check your implementation against the original find_max to make sure it still works.

  4. Compare find_max and find_max_pointer. Which of these functions do you think is more readable? Why?

  5. Write find_min and find_average functions that compute the minimum and average values of a given array. Write one function using array indexing and the other with pointer operations only.

  6. Write an additional procedure, find_stats, that computes the maximum, minimum, and average values in an array, all within one function. Since multiple values are to be computed and used by the main program, this function must pass values back using reference parameters. Use pointer operations to walk through the array. The function declaration should be:

    void find_stats(double* array, int array_size, double* max, double* min, double* average);