求结构体的偏移量方法

在看网关代码的时候进程看到类似于这样的语句:

typedef struct DeviceInfoDetailTable_t{
	struct DeviceInfoDetailTable_t *Next;
	u16	Len;
	u16	Index;
	u8	extAddr[8];
	u8	EndPoint;
	u8	deviceName[16];
	u16 AppDeviceID;
	u16	DeviceType;
	u8	roomNo;
	u8	minRange;
	u8	maxRange;
	//u32	standarIRno;
	u8	Reserved[4];
	u8	InClustersNum;
	u8  OutClustersNum;
	u16 IOClusters[20];
} DeviceInfoDetailTable_Typedef;

( SearchDataBase_Ext1(DeviceInfoDetailTable, (u8 *)&ZCL_Server_p->DestinationAddress[0], 64, 
  		   			(u16)&(((DeviceInfoDetailTable_Typedef *)0) -> extAddr), (u8 *)&DevInfoDetail_buf) == 0 )

里面的参数就是用来去结构体的偏移地址用的:

(u16)&(((DeviceInfoDetailTable_Typedef *)0) -> extAddr)


解释如下:

把0强制转换成类型DeviceInfoDetailTable_Typedef的指针操作:(DeviceInfoDetailTable_Typedef *)0,得到基址为0的结构体;

则通过&(((DeviceInfoDetailTable_Typedef *)0) -> extAddr),就得到了元素extAddr的地址,由于基址为0,所以偏移量被计算出来。


类似的可以使用宏来使用计算偏移量:

#define STRUCT_OFFSET(id, element) ((unsigned long) &((struct id*)0)->element)


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值