offsetof(struct foo, bar.mem)?

  • Thread starter Michael B Allen
  • Start date
M

Michael B Allen

Can offsetof be used to determine the offset of a member within an
embedded struct member?

For example, let 'struct foo' be a structure with an embedded structure
'struct bar' which has a member 'mem'. Can one do:

offsetof(struct foo, bar.mem)

Thanks,
Mike
 
P

Peter Nilsson

Michael said:
Can offsetof be used to determine the offset of a member within an
embedded struct member?

For example, let 'struct foo' be a structure with an embedded structure
'struct bar' which has a member 'mem'. Can one do:

offsetof(struct foo, bar.mem)

7.17p3...

The macros are ... ; and

offsetof(type, member-designator)

which expands to an integer constant expression that has type
size_t, the value of which is the offset in bytes, to the
structure member (designated by member-designator), from the
beginning of its structure (designated by type). The type and
member designator shall be such that given static type t;
then the expression &(t.member-designator) evaluates to an address
constant. (If the specified member is a bit-field, the behavior
is undefined.)

Technically, I'd say no, but there is a sure fire way...

offsetof(struct foo, bar_member) + offsetof(struct bah, mem)
 
M

Michael B Allen

7.17p3...

The macros are ... ; and

offsetof(type, member-designator)

which expands to an integer constant expression that has type
size_t, the value of which is the offset in bytes, to the
structure member (designated by member-designator), from the
beginning of its structure (designated by type). The type and
member designator shall be such that given static type t;
then the expression &(t.member-designator) evaluates to an address
constant. (If the specified member is a bit-field, the behavior
is undefined.)

Technically, I'd say no, but there is a sure fire way...

offsetof(struct foo, bar_member) + offsetof(struct bah, mem)

Actually from the wording "expression &(t.member-designator) evaluates
to an address constant" I would say it IS ok as &(t.bar.mem) is a valid
expression.

Mike
 
P

Peter Nilsson

Michael said:
Actually from the wording "expression &(t.member-designator) evaluates
to an address constant" I would say it IS ok as &(t.bar.mem) is a valid
expression.

But bar.mem is not a member of struct foo, bah is; mem is a member of
whatever struct bar is.

You're probably better off asking csc for the committee's intent, but
I think that in the rare circumstances where I might need such an
offset,
I'd have an object handy, and I can use that...

#define obj_offsetof(obj, mem) \
((size_t) ((char *) &(obj) - (char *) &(obj).mem))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top