结构体成员地址在结构体内的偏移

来源:互联网 发布:英雄古达剧情知乎 编辑:程序博客网 时间:2024/04/25 23:29

例:

typedef  struct 

{

unsigned shortcrc;                 /* CRC16 over data bytes*/

unsigned chardata[1];          /* Environment data  - variable lenth */

}env_t;

求data[0]偏移地址          =>


offset = (unsigned long )((env_t*)0)->data[0];


使用宏写成通用的方式  =>

#define struct_off(type, field)       (unsigned long)( (type*)0 )->field

1 0
原创粉丝点击