Function pointer explanation
** 1. How to get a function pointer
2. How to write function pointer variables*
3. Function pointer function
4. Application scenario of function pointer**
1. How to get the function pointer? There are two ways:
(1) Take function name
(2) & function name
2. How to write function pointer variables? Let me show you two examples. I believe you will understand!
(1) Create two functions
int add(int x, int y) { return x + y; }
void func() { return; }
(2) Write function pointer
int(*p)(int, int) = add; void(*p2)() = func;
p and p2 Is a function pointer variable, and the types of variables are int(*)(int,int),void(*)();
Note: since they are different types, they cannot be assigned to each other
3. Function pointer function
Unlike other pointers, function pointers cannot perform addition, subtraction, dereference, [] operations and address fetching operations, but function pointers have functions they do not have, namely () operation (i.e. function call operation).
4. Application scenario of function pointer
(1) The effect of "callback" is realized through the function pointer
Callback function is a function called through a function pointer, and the specific call time is determined by the system or library; The callback function is like int(*Cmp)(int x, int y); Next, I'll show you the code of bubble sorting using callback function
typedef int(*Cmp)(int x, int y); //If the comparison rule is met, 1 is returned; otherwise, 0 is returned void bubbleSort(int arr[],int len,Cmp cmp) { for (int bound = 0; bound < len; bound++) { for (int cur = len - 1; cur > bound; cur--) { if (cmp(arr[cur - 1], arr[cur]) != 1) { int tmp = arr[cur - 1]; arr[cur - 1] = arr[cur]; arr[cur] = tmp; } } } } //Less indicates ascending order. If x is less than y, it meets the sorting requirements int less(int x, int y) { return x < y ? 1 : 0; } //Greater indicates descending order. If x is greater than y, it meets the sorting requirements int greater(int x, int y) { return x > y ? 1 : 0; }
When the user enters the corresponding sorting method in the main function, different sorting results will appear.
(2) The programming mode of "transfer table" (table driven mode) is realized through function pointer
Suppose we want to implement a calculator, the most basic thing is to realize the operations of addition, subtraction, multiplication and division; Next, let's prepare these four functions
int add(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int division(int x, int y) { return x / y; }
Then the calculator operation is realized in the main function
//1. Let the user enter two integers int x = 0; int y = 0; printf("Please enter two integers:"); scanf("%d %d", &x, &y); //2. The user inputs the operation to be performed printf("Please enter operation 1 to select.Indicates addition, 2.Represents subtraction, 3.Represents multiplication, 4.Representation Division\n"); int choice = 0; scanf("%d", & choice); int ret = 0; if (choice == 1) { ret= add(x , y); } else if (choice == 2) { ret = sub(x , y); } else if (choice == 3){ ret = mul(x ,y); } else if (choice == 4) { ret = division(x,y); } else { printf("Your input is incorrect!"); } } } } printf("ret=%d\n", ret);
When we see this code, we will find that there are many conditional judgments, so it is inconvenient for us to understand and the readability is poor (that is, its cycle complexity is large). Therefore, in order to reduce the cycle complexity, we can implement it by "transfer table"! Next, let's look at how to implement the calculator by using the transfer table?
First, we need an array of function pointers
Func arr[] = { NULL, add, sub, mul, division };
The overall implementation is as follows
int add(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int division(int x, int y) { return x / y; } typedef int(*Func)(int, int); int main() { //1. Let the user enter two integers int x = 0; int y = 0; printf("Please enter two integers:"); scanf("%d %d", &x, &y); 2.The user enters the operation to be performed printf("Please enter operation 1 to select.Indicates addition, 2.Represents subtraction, 3.Represents multiplication, 4.Representation Division\n"); int choice = 0; scanf("%d", &choice); Func arr[] = { NULL, add, sub, mul, division }; int ret = arr[choice](x, y); printf("ret=%d\n", ret);
Obviously, there are not so many judgment conditions, so it is very convenient for us to read the code! At the same time, when we need to expand other functions (such as remainder, power, etc.), we only need to write the corresponding function in the outer layer, and then add the corresponding function pointer to the function pointer array, instead of adding conditional judgment by if else;