函数作为结构体成员变量

#include<stdio.h>

typedef struct A
{
int a;
char b;
} foo;
int f2()
{
printf("this is f2\n");
return 2;
}

void f3(int x,char y)
{
printf("this is f3 + %d\t +%c\n",x,y);
}

int main(int argc, char *arg[])
{
foo f1[]=
{
{f2(),'A'},
{f2(),'B'},
{f2(),'C'}
};

f3(f1[1].a,f1[1].b);

}

 

其中f2()这个函数作为f1()这个结构体的成员变量,打印结果如下:

 

-bash-3.2$ ./a.out
this is f2
this is f2
this is f2
this is f3 + 2 +B

可以看到,当定义f1这个数组的时候,f2()这个函数被执行了3次。

转载于:https://www.cnblogs.com/miry/p/5583003.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值