Section 29.1 Function Pointers
Just like
int *p; declares
p as a pointer to an
int, we can declare a pointer to a function. The syntax looks a little unusual at first:
void (*functionptr)(int);
This declares
functionptr as a pointer to a function that takes a single
int argument and returns nothing (
void). Just like arrays, function names themselves act as addresses, so both of the following work to assign a function pointer:
functionptr = &printNumber; /* explicit address-of */
functionptr = printNumber; /* implicit conversion, same effect */
To actually call the function through the pointer, we
dereference it (though C is forgiving here, and
functionptr(x) works too):
(*functionptr)(my_number);
Let’s see this in action:
Compile and run the code below in your own C environment.
#include <stdio.h>
/* Prototypes */
void printNumber(int);
/* Main function*/
int main(void) {
int my_number = 5;
//int *numberptr = &my_number;
void (*functionptr)(int) = &printNumber; //explicit address of the function
(*functionptr)(my_number);
void (*functionptr2)(int);
functionptr2 = printNumber; //implicit conversion
functionptr2(10);
//printNumber(my_number);
return(0);
}
void printNumber (int number){
printf("My number is %d.\n", number);
}
A function pointer can be reassigned to point to a
different function at any time, as long as that function has a matching prototype (same return type and same parameter types). This is exactly what lets us pick a function based on user input, rather than hard-coding an
if/
else that calls one function or the other directly:
Compile and run the code below in your own C environment.
#include <stdio.h>
int add(int, int);
int mult(int, int);
/* Main function*/
int main(void) {
int a, b, result, op;
int (*fptr)(int, int);
a = 5;
b = 4;
printf("What operation? 1 for addition, 2 for multiplication ");
scanf("%d",&op);
if(op==1){
fptr = &add;
}
else if(op==2){
fptr = &mult;
}
result = (*fptr)(a,b);
printf("Result of operation is %d\n", result);
/*
fptr = &add;
result = (*fptr)(a,b);
printf("Results of addition: %d\n", result);
fptr = &mult;
result = (*fptr)(a,b);
printf("Results of multiplication: %d\n", result);
*/
return(0);
}
int mult(int num1, int num2){
return num1 * num2;
}
int add(int num1, int num2){
return num1 + num2;
}
Activity 29.1.
Define two functions,
float square(float x) and
float cube(float x). Ask the user to enter a number and choose whether they want to square (2) or cube (3) it. Declare a function pointer, and use an
if/
else statement to assign it to the correct function based on the user’s choice. Finally, use your function pointer to calculate and print the result.
Compile and run the code below in your own C environment.
#include <stdio.h>
/* Prototypes for square and cube functions */
int main(void){
float number, result;
int choice;
float (*operation)(float);
printf("Please enter a number: ");
scanf("%f", &number);
printf("Do you want to square (2) or cube (3) this number: ");
scanf("%d", &choice);
/* Use an if statement to assign the correct function pointer to operation*/
if(...
/* Calculate the result using the function pointer operation and print it */
result = ...
printf("The result of the operation is %.2f.\n", result);
return 0;
}
//functions go here
Activity 29.2.
Let’s use a function pointer to convert a whole
batch of temperatures! We have two functions,
float cTof(float c) and
float fToc(float f). Ask the user which conversion they want to do (1 for Celsius→Fahrenheit, 2 for Fahrenheit→Celsius), declare a function pointer, and assign it to the correct function based on the user’s choice. Then write a
for loop that iterates through the temperature array, passing each value to your function pointer and printing the result.
Compile and run the code below in your own C environment.
#include <stdio.h>
/* Prototypes for square and cube functions */
float cTof (float temp);
float fToc (float temp);
int main(void){
float temp[] = {2.3, 5.4, 34.3, 2.8, 12.0, 6.0, 27.6, 19.9, 20.3, 15.1};
int size = 10;
int choice, i;
float tempconv;
/* Declare function pointers used to point to cTof or fToc functions*/
...
printf("Do you want to convert to Celcius (1) or to Fahrenheit (2)? ");
scanf("%d", &choice);
/* Use an if statement to assign the correct function pointer to operation*/
if ...
/* Loop to calculate and print all the results */
printf("Converted temperatures: ");
for...
return 0;
}
//Functions go here
float fToc (float temp){
//Write the temperature conversion
return (temp-32)*5.0/9.0;
}
float cTof (float temp){
//Write the temperature conversion
return temp*9.0/5.0+32;
}