偏移量
存储单元的实际地址与其所在段的段地址之间的距离。在结构体中理解为结构体内存中的单元到结构体起始位置的距离。
offsetof
MSDN上解释为:size_t offsetof( structName, memberName );
它的头文件为<stddef.h>,本质上是一个宏(注意不是函数)
第一个放结构体的类型名,第二个放结构体中的变量。
#include <stdio.h>
#include <stddef.h>
struct S1
{
char a;
int b;
char c;
};
int main()
{
printf("%d\n", offsetof(struct S1, a));
printf("%d\n", offsetof(struct S1, b));
printf("%d\n", offsetof(struct S1, c));
return 0;
}
输出结果如下,为结构体中变量相对于首地址的偏移量。