function pointer and pointer function __ function

Source: Internet
Author: User

Suppose you want to design a function called estimate (), estimating the amount of time it takes to write code for a specified number of lines, and expect the function to be used by different programmers. A portion of the code in Estimate () is the same for all users, but the function allows each programmer to provide its own algorithm to estimate the time it takes to run the algorithm.

To achieve this goal, the mechanism used is to pass the address of the algorithm function that the programmer wants to use to estimate (). Therefore, you must be able to complete the following tasks:
(1) Obtain the address of the function;

(2) Declaring a function pointer;

(3) Use function pointers to invoke functions.

Double pam (int);
Double (*PF) (int);  PF points to a function ' return double
double *pf (int);    PF () A function that returns a pointer-to-double

The signature and return value types of Pam () must be the same as PF.

PF = pam;               PF now points to the PAM () function
Double x = pam (4);      Call Pam () using the function name
double y = (*PF) (5);    
Double y = pf (5);       Also call Pam () using the pointer pf
This means PF = = (*PF).



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.