Function pointerIt is a pointer in C, C ++, D language, other Class C language, and Fortran 2003. Function pointers can be used to call functions and PASS Parameters like common functions. In languages such as C, function pointers can simplify code by providing a simple method for selecting and executing functions.
Function pointers can only point to functions with specific features. Therefore, all functions used by the same pointer must have the same parameters and return types.
URL: http://www.cnblogs.com/archimedes/p/3669872.html.
The following code illustrates a method for initializing a function pointer:
f( (*fp)() = &= f(= (*pf)(= pf();
The following is a simple application of function pointers in C:
#include <stdio.h> max( x, x > y ? main( (* p)(, ) = & max; , & a, & b, &= (* p)(( *
Node* search_list(Node* node, (node !=(node->value === node->
A more common method is to enable the function to use the linked list of any type of value. You must modify the function to make it irrelevant to the type. The solution is to use the function pointer.
#include <stdio.h>*search_list( Node *node, * (*compare)( *, *( node !=( compare( &node->value, value ) == = node->
When comparing a specific linked list, you need to compile an appropriate comparison function and pass the pointer pointing to the function and the pointer pointing to the value to be searched to the search function, the following is a comparison function found in an integer linked list:
compare_ints( *a, *( *( *)a == *( * desired_node = search_list(root, &desired_value, compare_ints);
If you want to search in a string linked list, the following code can complete the task:
#include<.h>= search_list(root, , strcmp);
Let's look at a complete example:
#include<stdio.h> object_compare( * a, * a->data < z->data ? : *maximum( * begin, * end, (* compare)( *, * * result =(begin !==++ main( data[] = {{}, {}, {}, {}, {}, {}, {}, { *= maximum(data + , data + , &, (*
2. Transfer table
The following Program is an example of a simplified conversion from an operator to an operation:
#include<stdio.h> _add(, _sub(, _mul(, _div(, _add( a, a + _sub( a, a - _mul( a, a * _div( a, a / main(= = , &= (====
You can use a conversion table to implement the same task. The conversion table is an array of function pointers. The Code is as follows:
#include<stdio.h> _add(, _sub(, _mul(, _div(, _add( a, a + _sub( a, a - _mul( a, a * _div( a, a / main(= = , & (*oper_func[])(, ) ==