宏offsetof作用是,计算结构体中某变量相对于首地址的偏移。
目录
1.宏offsetof()
声明
offsetof(type, member-designator)
参数
type — 这是一个 class 类型,是结构体名称
member-designator — 这是一个 class 类型的成员指示器。
返回值
该宏返回类型为 size_t 的值,表示 type 中成员的偏移量。
头文件
<stddef.h>
2.宏offsetof()的实现
#include<stdio.h>
#include<stddef.h>
struct stu
{
int a;
char b;
double c;
};
#define OFFSETOF(struct_type,mem_name) (size_t)&(((struct_type*)0)->mem_name)
int main()
{
printf("%d\n", OFFSETOF(struct stu, a));
printf("%d\n", OFFSETOF(struct stu, b));
printf("%d\n", OFFSETOF(struct stu, c));
return 0;
}
3.对于宏offserof()实现的理解
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/87397.html