函数指针

来源:互联网 发布:mac电脑中病毒 编辑:程序博客网 时间:2024/06/09 23:30

函数指针的声明:

函数类型(标识符指针变量)(形参列表);

注意:括号不能去掉,否则就变成指针函数了。

使用规则:

int func(int x);//声明一个函数

int (*f) (int x); //声明一个函数指针

f=func;          //将func函数地 首地址赋给指针f

举例说明:

#include <iostream>
using namespace std;

int max(int x,int y)

{
   return (x>y?x:y);
}

int main()
{
 int max(int x,int y);
 int (*f)(int x,int y);
 f=max;                     //表示方式1
 int a,b,c,d;
 cout<<"please input three integer:"<<endl;
 cin>>a>>b>>c;
 d=(*f)((*f)(a,b),c);    //函数指针的使用
 cout<<d<<endl;
 return 0;
}

 

#include <iostream>
using namespace std;

int max(int x,int y)

{
   return (x>y?x:y);
}

int main()
{
 int max(int x,int y);
 int (*f)(int x,int y)=&max;    //表示方式2
// f=max;
 int a,b,c,d;
 cout<<"please input three integer:"<<endl;
 cin>>a>>b>>c;
 d=(*f)((*f)(a,b),c);
 cout<<d<<endl;
 return 0;
}

 指针函数与函数指针的区别:指针函数,本质上是一种函数,只不过函数地返回类型是某一类型的指针。 

指针函数的声明:

函数类型 标识符指针* 函数名(形参列表);

 

0 0
原创粉丝点击