How to call the function of implementation code by function pointer

  • 2020-04-02 01:24:57
  • OfStack

Description:
A pointer can not only point to an integer, floating point, character, string variable, but also to an array, and can also point to a function.

A function is assigned an entry address at compile time. The function entry address is called the function pointer. You can point to a function with a pointer variable and then call the function from that pointer variable.

The method to define the pointer variable to the function is:


int (*p) (int ,int );

Int [function type pointed to by pointer variable p] (*p) [p is pointer variable pointed to by function] (int,int) [parameter type pointed to by p];

Compare with the prototype of the function


int max  (int, int );

Int [function type] Max [function name] (int,int) [function parameter type];

An example:
General method code:


#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;
}

We then define a pointer variable that points to the Max function and then call the function from that pointer variable.
Call the function by (*p)

#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;
}

Functions can be called directly through pointer p

#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;
}

Takes a pointer to a function as an argument
One of the most common USES of a function pointer variable is as an argument to a function, passing the function name to the parameter of another function. So that you can call different functions in the process of calling a function, given different arguments.

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

Analysis: I'm going to write three functions f1,f2, and f3 to evaluate the three functions x plus 1,2,x plus 3,x squared plus 1.

Then write a generic function called Squar, which has two parameters: the a power and the pointing function,
Program code:


#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;   
}


Related articles: