[英]Finding relative offset in nested structure
So, offsetof(struct, field)
returns the relative offset of field inside a plain structure. 因此,
offsetof(struct, field)
返回普通结构中字段的相对偏移量。 But is there a way to get the relative offset of a field inside of a nested structure. 但有没有办法获得嵌套结构内的字段的相对偏移量。
eg 例如
struct my_struct {
int a;
struct {
int b;
int c;
} anonymous_struct;
}
Is there any way to get the offset of b
and c
relative to my_struct
(at runtime). 有没有办法获得相对于
my_struct
的b
和c
的偏移量(在运行时)。
Yes, you can still use offsetof
. 是的,你仍然可以使用
offsetof
。
Eg 例如
size_t boff = offsetof(struct my_struct, anonymous_struct.b);
The requirements of offsetof
are that the type and member-designator must be such that given static
type t;
offsetof
的要求是类型和成员指示符必须是给定static
类型 t;
, &(t.
member-designator )
evaluates to an address constant. ,
&(t.
-designator )
计算地址常量。 The member-designator doesn't have to be a simple identifier. 成员指示符不必是简单的标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.