简体   繁体   English

在嵌套结构中查找相对偏移量

[英]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_structbc的偏移量(在运行时)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM