求结构体的偏移量方法

来源:互联网 发布:smo算法 python实现 编辑:程序博客网 时间:2024/09/21 09:00

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

typedef struct DeviceInfoDetailTable_t{struct DeviceInfoDetailTable_t *Next;u16Len;u16Index;u8extAddr[8];u8EndPoint;u8deviceName[16];u16 AppDeviceID;u16DeviceType;u8roomNo;u8minRange;u8maxRange;//u32standarIRno;u8Reserved[4];u8InClustersNum;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)


 



0 0
原创粉丝点击