1. function pointer definition and initialization:
INT (* p) (INT, INT) = NULL; P = maxvalue; // pass the maxvalue address of the function to P or (* P) = maxvalue; 2. Function callback: int getvalue (int A, int B, INT (* p) (INT, INT )); // function pointer as the getvalue parameter function pointer pointing to variable function 3. Dynamic sorting (changing sorting conditions) encapsulates conditions that determine the sorting method into a function, then callback typedef bool (* p_fun) (INT, INT); // header file # include <stdbool. h> void sortarray (int * arr, int count, p_fun P); 4. The return value of the function is the function pointer p_fun getfunctionbyname (char * name) use the function name to find the corresponding function namefunctionlist [] = {
{"Max", maxvalue },
{"Min", minvalue },
{"AVG", avgvalue },
{"Sum", sumvalue },
{"Mul", mulvalue}
};
P_fun getfunctionbyname (char * name)
{
For (INT I = 0; I <(sizeof (list)/sizeof (list [0]); I ++)
{
If (! Strcmp (list [I]. Name, name ))
{
Return list [I]. function;
}
}
Return maxvalue;
}
Function pointer, callback, dynamic sorting, and return function pointer