1.
Offsetof函数
size_t offsetof( structName, memberName );
代码如下:
//1.写一个宏,计算结构体中某变量相对于首地址的偏移,并给出说明
//考察:offsetof宏的实现
#define OFFSETOF(STN,MEN) (int)&(((struct S*)0)->MEN)
struct S
{
int a;
char b;
short c;
int d;
};
int main()
{
printf("%d\n", OFFSETOF(struct S, a));
printf("%d\n", OFFSETOF(struct S, b));
printf("%d\n", OFFSETOF(struct S, c));
printf("%d\n", OFFSETOF(struct S, d));
return 0;
}