C function pointer usage

Source: Internet
Author: User

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[])(, ) == 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.