Computer Science 340
Programming Languages

Fall 2019, Siena College

Lab 4: Callback Functions in C
Due: 11:59 PM, Tuesday, November 12, 2019

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.

Practice Program: Add three new command-line options to this program. One is a required parameter specifying the name of an output file, to be specified with -o or -output-file. The second is a flag that would turn on a "debug mode" in the program by setting a variable debug to 1 (its default should be 0 if the flag is not specified), to be specified with -d or -debug. The last is a number which represents the enrollment in a class, is required, and must be positive, to be specified with the -e or -enrollment flags. (10 points)

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.

Practice Program: Add functionality to that example. Your program should be able to sort floating point values either in ascending or descending order, and should introduce the ability to read in and sort at least one new data type (a struct that holds some non-trivial data) by at least two different criteria. (15 points)

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.

Practice Program: Add a callback function find_max and a call to sll_visit_all that demonstrates its use to the slltest.c program in the above example. (5 points)

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:

> FeatureValueScore
getopt-ex enhancements 10
qsort enhancements 15
sll enhancements 5
Total 30