DPST1091 Revision Pointers Exercises
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