在看网关代码的时候进程看到类似于这样的语句:
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)