函数为核心

函数传参 传入函数 

要想传入函数 即传入该函数的地址,注意形参的类型应与函数的地址类型一致

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))                          //函数指针

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值