First, the function pointer
Simply declaring a function pointer does not mean that it can be used immediately, as with other pointers, you must initialize a function pointer to a function before you perform a simple access to it.
int f (int);
Int (*PF) (int) =&f;
The second declaration creates a function pointer pf and initializes it to a pointer to function f. The initialization of a function pointer can also be done with an assignment statement. It is important to have a prototype of F before the initialization of the function pointer, otherwise the compiler cannot check whether the type of f is consistent with the type that PF points to.
The & operator in the initialization expression is optional, because the function name is always used by the compiler to convert it to a functional pointer. The & operator simply shows the tasks that the compiler will perform implicitly.
After the function pointer is declared and initialized, we can invoke the function in three ways:
int ret;
Ret=f (30);
Ret= (*PF) (30);
RET=PF (30);
The first statement simply invokes the function f with the name, but it may be executed in a different way than we thought. The function name F is first converted to a functional pointer that specifies the position of the function in memory. The function call operator then invokes the function and executes the code that starts at that address.
The second statement performs a simple access operation to PF, which converts the function pointer to a function name. This conversion is not really needed because the compiler will convert it back before executing the function call operator. However, the effect of this statement is exactly the same as the first statement.
The third statement has the same effect as the first two statements. Simple access is not required because the compiler needs a function pointer. This example shows how a function pointer is usually used.
So when should we use a function pointer? The two most common uses are to pass function pointers as parameters to functions and to convert tables.
Second, callback function
the callback function , as the name implies, is that the user defines a function by itself, the user implements the program content of the function, and then passes the function as a parameter to another (or system) function, which is called by the function of another (or system) at runtime. Functions are implemented by you, but are called by other (or system) functions at run time through parameter passing, which is called a callback function. Simply put, it is the function that you implement when you run the function of someone else.
in the system programming perspective: when the program is running, the application (application programs) often invokes the pre-prepared functions in the library via API. However, some library functions require the application to pass it a function, which is called at the appropriate time to accomplish the target task. The passed-in, then-called function is called a callback (callback function).
An example of using a function pointer is a callback function.
For example, the following function:
node* search_ist (Node *node,void const *value,int (*compare) (void const *,void const *))
{
while (node (!=null) {
if (compare (&node->value,value) ==0)
{
Break
}
node=node->link;
}
return node;
}
This is a type-independent list lookup, where the function pointer of the parameter function is used to compare the values stored in the linked list type.
In the upper function we pass a function pointer as a parameter to the other function, which will callback the user's function. This technique can be used at any time if the function that we are writing must be able to perform different types of work at different times or perform the work of a function caller only. Many windowing systems use callback functions to link multiple actions, such as dragging the mouse and clicking a button to specify a specific function in the user program.
It can also be used in a particular linked list to find:
int compare_ints (void const *a,void const *b)
{
if (* (int) A = = * (int *) b)
return 0;
Else
return 1;
}
This function can be used as follows:
Desired_node=search_list (root,&desired_value,compare_ints);
If you want to find a string in a linked list, you can do the following:
Desired_node=search_list (Root, "Desired_value", strcmp);
However, some compilers may issue warnings because strcmp parameters are declared as char* instead of void*;
Third, the conversion table
If you have the following code:
Switch (OPER) {
Case ADD:
Result=add (OP1,OP2);
Break
Case SUB:
Result=sub (OP1,OP2);
Break
Case MUL:
Result=mul (OP1,OP2);
Break
Case DIV:
Result=div (OP1,OP2);
Break
...
This switch statement will be very long for a few hundred operator calculators.
In order to use the switch statement, the code that represents the operator must be an integer. If they are zero-based integers, we can use the conversion table to accomplish the same task. A conversion table is an array of function pointers.
Creating a conversion table requires two steps. First, declare and initialize an array of function pointers. The prototype of the function must appear before the declaration of this array.
Double add{double,double};
Double sub{double,double};
Double mul{double,double};
Double div{double,double};
...
Double (*oper_func[]) (double,double) ={
Add,sub,mul,div,...
};
The correct order of functions in the initialization list depends on the shaping code used in the program to represent each operator. This example assumes that add is 0,sub is 1,mul is 2,div is 3, and so on.
The second step is to replace the entire previous switch statement with this one.
Result=oper_func[oper] (OP1,OP2);
Oper selects the correct function pointer from the array, and the function call operator executes the function.
This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1768751
C function pointers and callback functions