DPST1091 Revision Pointers Sample Solutions

Revision Exercise: Array Print Pointer (●●◌)

Download array_print_pointer.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/24T3/activities/array_print_pointer/array_print_pointer.c .

Your task is to add code to this function in array_print_pointer.c:

// Assumes that the pointer is aimed at one of the array elements.
void array_print_pointer(int nums[LENGTH], int *last) {
    printf("I should output some elements of the array.\n");
}

The above file array_print_pointer.c contains a function array_print_pointer, which should print out the elements of an array from the first element up to and including the element referenced by the pointer "last", which is an input to the function.

Each element printed should be followed by a space.

For this exercise, your task is to complete this function.

Note: you must not modify the array within the array_print_pointer function. You should only read the values in the array, not change them.

Note: All the arrays you are given will be of a fixed length, 10, which is defined as a constant.

Note: You can assume that the pointer "last" given as input to the function will always contain the address of one of the elements of the list.

The file also contains a main function which you can use to help test your array_print_pointer function. It has an example test case.

This main function will not be marked -- you must write all of your code in the array_print_pointer function.

You may modify the main function if you wish (e.g. to add further tests), but only the array_print_pointer function will be marked.

Once your program is working, the output from the provided test in the main function should be:

dcc -o array_print_pointer array_print_pointer.c 
./array_print_pointer 
1 2 3 4 5 6 

This diagram represents what the example above may look like in memory. In this diagram the computer placed the array starting at address 0xFFFF FF80, but this will almost certainly not be where it is placed when you run the program so do not use this address in your code.

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest array_print_pointer
Sample solution for array_print_pointer.c
// COMP1511 Array Print Pointer
// Print out the contents of an array, starting
// from index 0 and ending by printing out
// a particular element that is also being
// pointed at by a given pointer

// Marc Chee, March 2020

#include <stdio.h>

#define LENGTH 10

void array_print_pointer(int nums[LENGTH], int *last);

// This is a simple main function that you can use to test your array_print_pointer
// function.
// It will not be marked - only your array_print_pointer function will be marked.
//
// Note: the autotest does not call this main function!
// It calls your array_print_pointer function directly.
// Any changes that you make to this main function will not affect the autotests.

int main(int argc, char *argv[]){
   int nums[LENGTH] = {1,2,3,4,5,6,7,8,9,10};
   int *last = &nums[5];

   //Pass in the address of the sum and product variables
   array_print_pointer(nums, last);

   return 0;
}

// Print an array from the beginning until it reaches
// a pointer. Assumes that the pointer is aimed at one
// of the array elements.
void array_print_pointer(int nums[LENGTH], int *last) {
    int i = 0;
    int stop = 0;
    while (stop == 0 && i < LENGTH) { // printed i numbers
        printf("%d ", nums[i]);
        if (&nums[i] == last) { // last is aimed at nums[i]
            stop = 1;
        }
        i++;
    }
}