offsetof_C语言中文网
| 定义于头文件 |
||
| #define offsetof(type, member) /*implementation-defined*/ |
||
宏 offsetof 展开成 size_t 类型的整数常量表达式,其值为从指定类型对象起始到其指定成员的偏移,若填充存在则包含之。
示例
运行此代码
#include <stdio.h> #include <stddef.h> struct S { char c; double d; }; int main(void) { printf("the first element is at offset %zu\n", offsetof(struct S, c)); printf("the double is at offset %zu\n", offsetof(struct S, d)); }
可能的输出:
the first element is at offset 0 the double is at offset 8
参阅
| sizeof 运算符返回的无符号整数类型 (typedef) |