c语言结构体新玩法

今天介绍一种结构体赋值的新方法,适用于你想赋值部分成员时的情况
通常情况下

 struct animal dog = {“dog”,1,1,1}struct  Animal person={
		.peat  = personEat,
		.pbeat = personBeat,
	};

dem代码展示

#include <stdio.h>


struct Animal
{
	char name[128];
	int age;
	int sex;

	void (*peat)();
	void (*pbeat)();	
};

void dogEat()
{

	printf("dog  eat\n");
}

void catEat()
{

	printf("cat  eat\n");
}

void personEat()
{

	printf("person  eat\n");
}


void dogBeat()
{
	printf("dog beat\n");
}

void catBeat()
{
	printf("cat beat\n");
}

void personBeat()
{
	printf("person  beat\n");
}



int main()
{

	struct  Animal dog= {
		.peat = dogEat,
		.pbeat = dogBeat,

	};
	struct  Animal cat ={
		.peat = catEat,
		.pbeat = catBeat,
	};
	struct  Animal person={
		.peat  = personEat,
		.pbeat = personBeat,
	};
/*    struct Animal dog = {"dog",1,1,1,1,1}       */


/*
	dog.peat = dogEat;
	cat.peat = catEat;
	person.peat = personEat;*/




	dog.peat();
	cat.peat();
	person.peat();

	dog.pbeat();
	cat.pbeat();
	person.pbeat();
	/* code */
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值