So, offsetof(struct, field) returns the relative offset of field inside a plain structure. But is there a way to get the relative offset of a field inside of a nested structure.
e.g.
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).
Yes, you can still use
offsetof.E.g.
The requirements of
offsetofare that the type and member-designator must be such that givenstatictypet;,&(t.member-designator)evaluates to an address constant. The member-designator doesn’t have to be a simple identifier.