C 参考手册

位置:首页 > C 参考手册 >类型支持 > offsetof

定义于头文件 <stddef.h>
#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)