Function Pointer
A function pointer is a pointer variable that points to a function.
Typically, a pointer variable points to an integer, character, or array variable, whereas a function pointer points to a function.
A function pointer can be used to call a function and pass parameters just like a regular function.
Declaration of a function pointer variable:
typedef int (*fun_ptr)(int,int); // Declare a function pointer type that points to a function with the same parameters and return value
Example
The following example declares a function pointer variable p
that points to the function max
:
#include <stdio.h>
int max(int x, int y)
{
return x > y ? x : y;
}
int main(void)
{
/* p is a function pointer */
int (* p)(int, int) = & max; // & can be omitted
int a, b, c, d;
printf("Please enter three numbers:");
scanf("%d %d %d", & a, & b, & c);
/* Equivalent to directly calling the function, d = max(max(a, b), c) */
d = p(p(a, b), c);
printf("The largest number is: %d\n", d);
return 0;
}
Compiling and executing this code will produce the following output:
Please enter three numbers:1 2 3
The largest number is: 3
Callback Function
Function Pointer as an Argument to a Function
A function pointer variable can be used as an argument to a function. A callback function is a function that is called through a function pointer.
Simply put: A callback function is a function that you implement and is called by another function during its execution.
>
Here is an explanation from Zhihu author Chang Xiuling:
You go to a store to buy something, but the item you want is out of stock. So, you leave your phone number with the clerk. A few days later, when the store has the item in stock, the clerk calls you, and you come to the store to pick up the item. In this example, your phone number is called a callback function, leaving your phone number with the clerk is called registering a callback function, the store having the item in stock is called triggering the callback-related event, the clerk calling you is called invoking the callback function, and you coming to the store to pick up the item is called responding to the callback event.
Example
In this example, the function populate_array()
is defined with three parameters, where the third parameter is a function pointer used to set the values of the array.
We define the callback function getNextRandomValue()
, which returns a random value and is passed as a function pointer to the populate_array()
function.
populate_array()
will call the callback function 10 times and assign the return value of the callback function to the array.
#include <stdlib.h>
#include <stdio.h>
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
for (size_t i=0; i<arraySize; i++)
array[i] = getNextValue();
}
// Get a random value
int getNextRandomValue(void)
{
return rand();
}
int main(void)
{
int myarray[10];
/* getNextRandomValue cannot have parentheses, otherwise it won't compile because adding parentheses means passing an int instead of a function pointer */
populate_array(myarray, 10, getNextRandomValue);
for(int i = 0; i < 10; i++) {
printf("%d ", myarray[i]);
}
printf("\n");
return 0;
}
Compiling and executing this code will produce the following output:
16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709