应用场景:
#define offsetof(type, field) (long)&(((type*)0)->field)
type:表示一个结构体
filed:表示type结构体的成员
(type*)0:应该是表示结构体首地址
(type*)0)->field:表示结构体成员field
&(((type*)0)->field):表示成员field相对于结构体首地址的偏移地址
作用:计算出type结构体成员filed在结构体中的偏移地址。
如:struct str
{
int a;
int b;
int c;
}
则:offsetof(str,a)=0;offsetof(str,b)=4;offsetof(str,c)=8;
C 中&(type *)0)->field)的含义
最新推荐文章于 2022-03-03 19:31:36 发布