函数传参 传入函数
要想传入函数 即传入该函数的地址,注意形参的类型应与函数的地址类型一致
int fun( int ) //函数的地址是一个指针类型
{ }
因此该函数的形参类型为: 该函数的类型为 int(*)(int)
int fun1(int (*p) (int))
{ }
int main()
{
fun1(fun);
return 0;
}
函数返回值 返回函数
要想返回一个函数,即返回一个指针函数的类型
void fun(short a ,short b){ }
int main()
{
int (*p)(void)=test(fun);
}
则test要如何书写呢?
首先test的返回类型为 int(*)(void)
fun的地址类型为 void (*p)(short, short)=fun
故test 的类型可以为
int (*)(void) test(void(*p)(short,short)) //但是没有这种写法
我们应写成 int (* test(void(*p)(short,short)))(void) 这种形式
即
int (* test(void(*p)(short,short)))(void)
{ }
若我们想要返回p 则该函数应写成
void(*)(short ,short) //返回类型
test(void (*p) (short,short)) //传入类型
void(*)(short ,short) test(void (*p) (short,short)) //没有这种写法
void(* test(void (*p)(short,short)))(short ,short) //要这样写
分析 int (* test(void(*p)(short,short))) (void)
其返回类型为int(*)(void) //函数指针
其形参类型为 (void (*p) (short,short)) //函数指针