offerof函数是用来计算结构体某变量相对首地址的偏移
#include<stdio.h>
#include<stddef.h>
struct S {
char a;
int b;
long c;
};
int main()
{
printf("%d\n", offsetof(struct S, a));
printf("%d\n", offsetof(struct S, b));
printf("%d\n", offsetof(struct S, c));
}
编写宏,计算结构体某变量相对首地址的偏移
#include<stdio.h>
#define Offsetof(struct_name, member_name) (int)&(((struct_name*)0)->member_name)
struct S {
char a;
int b;
long c;
};
int main()
{
printf("%d\n", Offsetof(struct S, a));
printf("%d\n", Offsetof(struct S, b));
printf("%d\n", Offsetof(struct S, c));
}