A function pointer is a normal C pointer variable, but what it points to is a function. This allows for fancy and dynamic programs that will call different functions in different circumstances dynamically based on which ever function the pointer is pointing to at that time. The programmer creates logic to change the value of the pointer to various functions for different reasons, and calls which ever function is pointed to by the pointer. Lets demonstrate this with some examples.
Technical overview
We will cover the syntax of function pointers. It will let us know how we can cause the pointer to point to a function; in other words, how we can store the address of the function using the pointer. We will also learn the declaration and reference of the function pointer and how the function pointers can be used to make the function calls for the pointer function.
Syntax
The general syntax for the declaration of the function pointer is as follows:
The declaration method of the function pointer is as same as the usual function declaration; the only difference in the function pointer is when we declare the name of the function pointer, we use the symbol * and the variable is included in parenthesis, unlike the normal function where the name is declared without the use of any symbol with it. Let’s declare a dummy function pointer with the help of some hypothetical example; first, declare any function with the usual method, e.g., int func (int, int). After the declaration of function func, we will now declare a function pointer as int (*funcptr)(int, int);. Here is the general syntax and assignment of a function to a function pointer:
int func (int, int);
// function pointer declaration
int (*funcptr)(int, int);
// function pointer assignment
funcptr = func;
Example # 01
In this example we will create a function called square with 2 parameters for len and width calculation of a rectangle. [len * width = area]. We then declare a function pointer and assign the address of the square function to the function pointer called sp. After receiving dynamic user inputs we call the function by using the function pointer to make the function call execution.
In the body of the function, we will simply take the multiplication of both the variables len and width to compute the area of the rectangle and exit the function returning this area. The implementation of the examples is shown in the following snippet:
int square(int len, int width) {
int area = len * width;
return area;
}
int main() {
int l,w, area;
int (*sp)(int, int);
printf("Enter length and breadth of a rectangle: ");
scanf("%d%d", &l, &w);
// assign function to function pointer
sp=square;
// call function pointer
area = (*sp)(l,w);
printf("Area of rectangle = %d\n", area);
return 0;
}
Enter length and breadth of a rectangle: 10 4
Area of rectangle = 40
linuxhint@:~$
We have given the same user input for both the variables l and w as “5” since they both represent the length and width of the square, which are equal in value. The output has returned the square of value 5 as the area of the square.
Example # 02
With the help of this example, we will create an array of function pointers where every element of the array will be storing the pointer function for different functions. For example, we will declare the two normal functions of subtraction and addition with the return type integer with two function parameters having the integer data type. Then we will declare an array of function pointers and then initialize the two array members with our two functions declared for addition and subtraction. Then we will use the array of pointer function with the index of the array function pointer, e.g., “arr[i]”.
The addition and subtraction functions will use the read values of initialized variables a and b. We will then display these results by calling the printf() method. The following snippet shows the complete program for this example:
int addition (int a, int b) {
return a + b;}
int subtraction (int a, int b) {
return a - b;}
int main() {
int a, b;
int (*function [2]) (int, int);
function[0] = addition;
function [1] = subtraction;
printf ("Enter two values: ");
scanf ("%d %d", &a, &b);
int result = (*function [0]) (a, b);
printf ("Addition (a+b) = %d\n", result);
result = (*function [1]) (a, b);
printf ("Subtraction (a-b) = %d\n", result);
return 0;
}
Enter two values: 33
11
Addition (a+b) = 44
Subtraction (a-b) = 22
linuxhint@:~$
Conclusion
The guide contains a basic example and explanation of function pointers in the C language. Note to change the syntax as you change the parameter quantity and data type as well as if you change the return type of the function the function pointer will need to change respectively.