结构体类型使用心得

本文详细介绍了嵌入式开发中结构体的设计规范,包括类型命名规则、变量命名建议及结构体聚合原则,旨在提高代码的移植性和阅读性。

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

//结构体1
struct channel_t {
	unsigned char input;
	unsigned char output;
	unsigned char fault_output;
	unsigned char pulse;
};
typedef struct channel_t * channel_p;

struct do_info_t {
	struct rt_mutex mutex;
	struct  channel_t  channel[8];
};
typedef struct do_info_t* do_info_p;
/*结构体定义*/
struct module_info_t {
	struct rt_mutex mutex;
	unsigned char addr;
	unsigned char state;
	unsigned char type;
};
typedef struct module_info_t* module_info_p;

/*正常定义*/
struct rt_mutex module_info_mutex;
unsigned char module_addr;
unsigned char module_state;
unsigned char module_type;

使用心得

1.结构体类型命名不使用typedef指令,只可用在其指针类型上,后缀加"_p"
2.变量类型命名之后添加“_t”,用以区分变量类型和变量名
3.定义结构体变量名尽量短,可采用词语缩写,如curr,info等等
4.变量类型命名应准确表达出其含义,描述“what”,而非“how”
5.结构体可以将相关变量聚合到一起,提高移植性和阅读性,应尽量将相同类型封装进去结构体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值