首先我们应该特别留意 : offsetof 是一个宏,并非是一个函数 !
宏offsetof的介绍 :
参数:第一个是结构体类型名称,第二个是结构体成员名
返回类型:size_t无符号整形
引用的头文件:<stddef.h>
offsetof的使用举列 :
#include <stddef.h>
struct Stu // 注释为相对于起始位置的偏移量
{
int a;//0~3
char c;//4
//5~7
double d;//8~15
};
int main()
{
printf("%d\n", sizeof(struct Stu));
printf("%d\n", offsetof(struct Stu, a));
printf("%d\n", offsetof(struct Stu, c));
printf("%d\n", offsetof(struct Stu, d));
return 0;
}
offsetof的模拟实现 :
#include <stddef.h>
//写一个宏&#