关于struct的初始化 error 的问题C242: ‘struct‘: too many initializers

博客讨论了在C语言中使用结构体初始化时遇到的错误,即当初始化复杂结构体成员时,未使用花括号导致的编译错误。通过在初始化列表中加入花括号,成功解决了编译问题,强调了在初始化复杂结构体时使用花括号的必要性和良好实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没有加{ }进行初始化

typedef struct{
	uchar led_enable[9];
	uchar led_State[8];
	
	struct{
		uchar pwm_flags;
		uint pwm_flags_count;
		
		uchar pwm_count;
		uchar led_Cmp_Buff[8];
		uchar led_PWM_enable[9];
	}led_pwm_struct;
}led_def;

led_def led_struct = 
{
		{0,1,0,0,0,0,0,0,
		 1},
		{1,1,0,0,0,0,0,0},
	//	{
			0,
			0,
			0,
			{0,0,0,0,0,0,0,0},
			{0,1,0,0,0,0,0,0,
			 1},
	//	}
};

bsp_c\led.c(17): error C242: ‘struct’: too many initializers

加{ }进行初始化

typedef struct{
	uchar led_enable[9];
	uchar led_State[8];
	
	struct{
		uchar pwm_flags;
		uint pwm_flags_count;
		
		uchar pwm_count;
		uchar led_Cmp_Buff[8];
		uchar led_PWM_enable[9];
	}led_pwm_struct;
}led_def;

led_def led_struct = 
{
		{0,1,0,0,0,0,0,0,
		 1},
		{1,1,0,0,0,0,0,0},
		{
			0,
			0,
			0,
			{0,0,0,0,0,0,0,0},
			{0,1,0,0,0,0,0,0,
			 1},
		}
};

成功编译

结论:对于简单的可以不用加{},对于比较难的需要加上{},以便于识别,不过,最好还是加上为好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值