offsetof

offsetof

在头文件中定义
#define offsetof(type,member)/ *实现定义的* /

宏的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

扩展内容

size_t由sizeof运算符返回的无符号整数类型(typedef)

| 用于offsetof的C ++文档 |