Assignment 1: Function Pointers

Due by: Friday, January 24, 2025 at 11:59 p.m.

In this assignment, you will explore how to declare and work with C function pointers. For an overview of function pointers, consult the following references:

Function Pointer Syntax

To understand the syntax rules for function pointer declarations, consider a basic variable declaration:

int value;

The key observation is to note that all of the declaration components, including the type and the semicolon, are placed around the variable name. The same holds true for a function pointer declaration:

int (*function) (char);

In this declaration, the (*function) portion of the line declares that the function variable is a pointer to a function. Note that the parentheses and the * are required. The rest of the line specifies that it's a pointer to a function that takes a char parameter and returns an int. Note that replacing (*function) with a function name would make this line a standard function prototype.

Implementation Requirements

First, download the archive given below and unzip it into an appropriate directory.

Note: Part of the goal of this assignment is to get you accustomed to the development environment for this course and to get in the habit of using good software development practices. Testing is a fundamental skill that you need to develop if you have not already done so. Do not complete all of the steps at once. Instead, let the pre-defined test cases guide your development.

As a next step, read the tests provided in tests/public.c, then run make test. The first step below will fix one of these tests. Fix just enough code to pass the first test case (C_run_add). Save your code, recompile, and re-run the test suite. If your code passes this test, move on to the next step. Repeat this process (code, recompile, test) after each step of the assignment.

Development Steps

Fix the code in fptr.c and fptr.h so that it compiles cleanly without warnings. The specific steps you need to complete are as follows:

  1. Edit the run_add() function in fptr.c so that it declares a local variable and initializes it to point to the add() function defined in funs.c. Use the function pointer to call the function indirectly with the x and y parameters.
  2. Change the declaration of run_func() in both fptr.c and fptr.h so that a function name can be passed as the third parameter. Inside of run_func() call the passed function with the x and y parameters, returning the result.
  3. Change the declaration of get_func() in both fptr.c and fptr.h so that the function returns a function pointer. Hint: As described above, all the function pointer syntax needs to go around get_func (bool). After changing the declaration, make get_func() return add() if the passed choice was true and sub() otherwise.
  4. Finally, fix the code in main.c as indicated so that your submission passes the integration test provided.

Turn In

First, run make clean to remove all of the executable and object files from your directory structure. Then, zip up the contents of your project directory in either a .zip or a .tar.xz file. Upload this archive to Brightspace. Your assignment must be submitted by Friday, January 24, 2025 at 11:59 p.m.. Grace days are not available for assignments.

Although you won't be graded on style for assignments, now is a good time to get used to the GNU style described in Coding Standards.

All work must be done within assigned teams, though you may discuss general concepts with your classmates. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.

Under no circumstances should any member of one team look at the code written by another team. Tools will be used to detect code similarity automatically.

Code that does not compile will automatically score zero points.

Grading

Your grade will be determined by the following categories.

Category Description Weight
Compiling Code Free points! 15%
Unit Tests Your solution passes all three unit tests. 60%
Integration Test Your solution passes the integration test. 25%

Credits

This assignment is based on material from CS 361 by Michael S. Kirkpatrick. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.