Computer Science 340
Programming Languages
Fall 2019, Siena College
In this lab exercise, you will experiment with some more advanced usage of pointers in C, and their use as callback functions.
You may work in groups of size 2 or 3 on this lab. Only one submission per group is needed.
Getting Set Up
You will receive an email with the link to follow to set up your GitHub repository for this lab (callbacks-lab-yourgitname). If you are working on this problem set with a partner, one member of the group should follow the link to set up the repository on GitHub. Your problem set partner will receive a subsequent email with a link to follow that will grant them the rights to clone the repository and commit and push changes to the origin on GitHub.
Please answer lab questions in the README.md file in your group's GitHub repository.
Handling Command-Line Parameters
We have seen that you can retrieve the command line parameters to a
program through the argc and argv parameters to your
program's main function. You might have noticed that many
Unix commands make heavy use of
command-line parameters. These often are switches that start with
-
or --
. For example, see the man page for ls.
You can imagine that simply parsing the command line parameters to
decide what the command is supposed to do can be a complex task.
The C standard library provides a mechanism to parse these command-line parameters more easily and efficiently: getopt. See the description in section 3 of the manual: getopt(3).
The getopt-ex directory in your repository includes an example demonstrating some of the ways to make use of this utility.
Function Pointers/Callbacks
The PDF file in the qsort directory of your repository is an excerpt from the classic book The C Programming Language, Second Edition, by Kernighan and Ritchie, Prentice Hall, 1988. That book's Section 5.11 shows you a way you can write a sort function to sort data in multiple ways by specifying a function pointer as one of the parameters to the sort function. In this case, this would be a comparator function that knows how to compare two values during the swap procedure. This idea is formalized in Java with the Compatator interface, but as usual, in C, we can achieve similar functionality but without the formal language or API support.
Make sure you understand how this works, then look at the qsort function provided by the C standard library (man qsort). This works similarly to the one in K&R, but works on an arbitrary array of any data type.
The qsort_examples directory has a program that shows some examples of qsort in action.
For another way to use function pointers in a way somewhat like an iterator, see the function sll_visit_all and its usage in the example in the sll directory of your repository.
Submitting
Your submission requires that all required deliverables are committed and pushed to the master for your repository's origin on GitHub. That's it! If you see everything you intend to submit when you visit your repository's page on GitHub, you're set.
Grading
This assignment is worth 30 points, which are distributed as follows:
> Feature | Value | Score |
getopt-ex enhancements | 10 | |
qsort enhancements | 15 | |
sll enhancements | 5 | |
Total | 30 | |