offsetof(struct foo, bar.mem)?

Discussion in 'C Programming' started by Michael B Allen, Sep 2, 2005.

  1. 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
     
    Michael B Allen, Sep 2, 2005
    #1
    1. Advertising

  2. Michael B Allen wrote:
    > 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)

    --
    Peter
     
    Peter Nilsson, Sep 2, 2005
    #2
    1. Advertising

  3. On Thu, 01 Sep 2005 21:18:54 -0700, Peter Nilsson wrote:

    > Michael B Allen wrote:
    >> 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)


    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
     
    Michael B Allen, Sep 2, 2005
    #3
  4. Michael B Allen wrote:
    > On Thu, 01 Sep 2005 21:18:54 -0700, Peter Nilsson wrote:
    > > Michael B Allen wrote:
    > >> 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)

    >
    > 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))

    --
    Peter
     
    Peter Nilsson, Sep 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    384
    John Roth
    Jul 29, 2005
  2. Jonathan Bartlett
    Replies:
    7
    Views:
    454
    Peter Nilsson
    Jul 8, 2005
  3. Wejn
    Replies:
    2
    Views:
    121
    Michal
    Nov 29, 2003
  4. Max Williams
    Replies:
    10
    Views:
    204
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    316
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page