How to call a function (implementing code) through a function pointer _c language

Source: Internet
Author: User
Tags pow

Description
Pointers can not only point to an integer, float, character, string, or to an array, but also point to a function.

A function is assigned to an entry address at compile time. This function entry address is called a pointer to a function. You can point to a function with a pointer variable, and then call this function through the pointer variable.

The way to define a pointer variable to a function is:

Copy Code code as follows:

Int (*p) (int, int);

int "The type of the function to which the pointer variable P refers" (*p) "P" is the parameter type pointed to by the pointer variable "(int,int)" p of the function;

Compared to the prototype of the function

Copy Code code as follows:

int max (int, int);

int "function's type" max "function name" (int,int) "function's parameter type";

An example:
Code for the general method:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int max (int x,int y);
int a,b,c,m;
cout<< "Please input three integers:" <<endl;
cin>>a>>b>>c;
M=max (Max (a,b), c);
cout<< "max=" <<m<<endl;
return 0;
}
int max (int x,int y) {
int z;
if (x>y) {
Z=x;
} else{
Z=y;
}
return z;
}

Then we define a pointer variable, point to the Max function, and then call the function through the pointer variable.
to invoke a function by (*P)
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int max (int x,int y);
Int (*p) (int x,int y);
P=max;
int a,b,c,m;
cout<< "Please input three integers:" <<endl;
cin>>a>>b>>c;
M= (*p) (*p) (a,b), c);
cout<< "max=" <<m<<endl;
return 0;
}
int max (int x,int y) {
int z;
if (x>y) {
Z=x;
} else{
Z=y;
}
return z;
}

function can be called directly via pointer p
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int max (int x,int y);
Int (*p) (int x,int y);
P=max;
int a,b,c,m;
cout<< "Please input three integers:" <<endl;
cin>>a>>b>>c;
M=p (P (a,b), c);
cout<< "max=" <<m<<endl;
return 0;
}
int max (int x,int y) {
int z;
if (x>y) {
Z=x;
} else{
Z=y;
}
return z;
}

use pointers to functions as arguments to functions
One of the most common uses of function pointer variables is to pass function names to the formal parameters of other functions as arguments to the function. So that you can invoke a different function in the process of calling a function, depending on the arguments given.

For example, using this method to solve, two functions y1= (x+1) ^1;   Y2= (2x+3) ^2; y3= (x^2+1) ^3

Analysis: write 3 function f1,f2,f3 to find the x+1,2x+3,x^2+1 value of the above 3 functions.

Then write a universal function Squar, he has two parameters: A and point function,
Program code:

Copy Code code as follows:

#include <iostream>
#include <math.h>
using namespace Std;
Double fun1 (double n) {
Double R;
r=n+1;
return R;
}
Double fun2 (double n) {
Double R;
r=2*n+3;
return R;
}
Double Fun3 (double n) {
Double R;
R= (Pow (n,2) +1);
return R;
}
Double Squar (int A, double X, double (*p) (double)) {
Double r,z;
Z= (*p) (x);
R=pow (Z,a);
return R;
}
int main () {
Double fun1 (double n);
Double fun2 (double n);
Double Fun3 (double n);
Double Squar (int A, double X, double (*p) (double));
Double X;
cout<< "Please input x:";
cin>>x;
cout<< "(x+1) ^1=";
Cout<<squar (1,X,FUN1) <<endl;
cout<< "(2x+3) ^2=";
Cout<<squar (2,x,fun2) <<endl;
cout<< "(x^2+1) ^3=";
Cout<<squar (3,X,FUN3) <<endl;
cout<<endl;
return 0;
}

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.