Concepts
Function pointer is a kind of pointer whicn points to function. There two commom uses of function pointers, jump tables and passing a function pointer as an argument in a function call.
Uses&Examples
The pointer to function must be initialized to point to something before indirection can be performed on it, which means, to use function pointers, we should firstly have a function already declared, and then we can assign a pointer that fits this function to it.
int addValue(int a, int b) {
return a+b;
}
int (*pf)(int a, int b) = &addValue;
As we can see, the second declaration create pf, a pointer to function, with two arguments, making its type the same as function addValue. Thus we this initialization is avaliable. To use the function pointer, we have three ways.
int a = 20;
int b = 10;
int ans;
//1th method
ans = addValue(a, b);
//2nd method
ans = (*pf)(a, b);
//3nd method
ans = pf(a, b);
The outcome was shown in pf1.c
Callback Functions
The more important way of function pointer is callback function. we can implement an operation function, and we pass a function pointer to the operation, then we can decide indeed which operation is used when we call operation.
void operation(int a, int b, int(*op)(int, int));
we pass an function pointer to the operation function, and we can use the function pointer paseed in the block.
void operation(int a, int b, int(*op)(int, int)){
int ans = op(a,b);
printf("%d \n", ans);
}
Function Pointer Array
This time, we declare an array of function pointers, and initialize each of them
int (*pfArray[4])(int, int);
pfArray[0] = addValue;
pfArray[1] = subValue;
pfArray[2] = plusValue;
pfArray[3] = divValue;
int addValue(int a, int b) {
printf("a + b = ");
return a+b;
}
int subValue(int a, int b){
printf("a - b = ");
return a-b;
}
int plusValue(int a, int b){
printf("a * b = ");
return a*b;
}
int divValue(int a, int b){
printf("a / b = ");
return a/b;
}
And we can call them each time by just access each item of the array.
for (int i = 0; i < 4; i++){
operation(a, b, pfArray[i]);
}
Conclusion
From all above, we have had a basic knowledge of function pointers, and the executable code used in this report are as follows.
#include<stdio.h>
int addValue(int a, int b);
int subValue(int a, int b);
int plusValue(int a, int b);
int divValue(int a, int b);
void operation(int a, int b, int(*op)(int, int));
int main(){
int (*pf)(int a, int b) = &addValue;
int a = 20;
int b = 10;
int ans;
//Show the use of function pointer
//1th method
printf("call addValue(10, 20):\n");
ans = addValue(a, b);
printf("%d \n", ans);
//2nd method
printf("call (*pf)(10, 20):\n");
ans = (*pf)(a, b);
printf("%d \n", ans);
//3nd method
printf("call (*pf)(10, 20): \n");
ans = pf(a, b);
printf("%d \n", ans);
//Show callback function
printf("Call back function and function pointer array used:\n");
int (*pfArray[4])(int, int);
pfArray[0] = addValue;
pfArray[1] = subValue;
pfArray[2] = plusValue;
pfArray[3] = divValue;
for (int i = 0; i < 4; i++){
operation(a, b, pfArray[i]);
}
return 0;
}
int addValue(int a, int b) {
printf("a + b = ");
return a+b;
}
int subValue(int a, int b){
printf("a - b = ");
return a-b;
}
int plusValue(int a, int b){
printf("a * b = ");
return a*b;
}
int divValue(int a, int b){
printf("a / b = ");
return a/b;
}
void operation(int a, int b, int(*op)(int, int)){
int ans = op(a,b);
printf("%d \n", ans);
}