- 一道百度笔试题引入
- offsetof
一道百度笔试题引入
Question:
写一个宏,计算结构体中某变量相对于首地址的偏移,并给出说明。
offsetof
offsetof 官方文档(英文版)
size_t offsetof( structName, memberName );
头文件:
返回类型:size_t是unsigned int
功能:Return member offset
struct S1
{
char c1;// 1 8 1
int i; // 4 8 4
char c2;// 1 8 1
}s1;
int main()
{
printf("%un", offsetof(struct S1, c1));//0
printf("%un", offsetof(struct S1, i));//4
printf("%un", offsetof(struct S1, c2));//8
return 0;
}



