因此,offsetof(struct, field)返回普通结构内的字段的相对偏移量.但有没有办法获得嵌套结构内的字段的相对偏移量.
例如
struct my_struct {
int a;
struct {
int b;
int c;
} anonymous_struct;
}
Run Code Online (Sandbox Code Playgroud)
有没有什么办法让偏移的b和c相对my_struct(在运行时).
CB *_*ley 14
是的,你仍然可以使用offsetof.
例如
size_t boff = offsetof(struct my_struct, anonymous_struct.b);
Run Code Online (Sandbox Code Playgroud)
要求offsetof是类型和成员指示符必须是给定static类型t;,&(t.成员 - 指示符)评估地址常量.所述构件-指示符不必是简单的标识符.