一、指针函数
返回指针的函数
声明: 类型* 函数名(形参){}
double* Func(double a, double b) //指针函数
{
double z = (a > b) ? a : b;
return &z;//返回指针
}
void main() {
double x=3, y=5;
double* p = Func(x, y);
cout << "max=" << *p << endl;
system("pause");
}
输出结果: max=5
二、函数指针
指向函数入口地址的指针
声明: 类型 (*指针名)(形参);
int max(int x, int y) { return x > y ? x : y; }
void main() {
int (*fun)(int, int);//函数指针声明
fun = max;
int k;
k = max(2, 5); cout << k << endl; //函数名调用
k = fun(2, 5); cout << k << endl; //指针名调用
k = (*fun)(2, 5); cout << k << endl; //指针名调用
system("pause");
}
输出结果:
5
5
5