Printing offset of a member of structure

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jun 21, 2005.

  1. Guest

    I am trying to print the offset of a particulat member in
    a structure, but it's not working. I am using the following
    expression to print the offset.

    &(struct my_struct *)0->member_name

    What's wrong with this ?

    If I use,
    &((struct my_struct *)0)->member_name, it works. But it seems
    to be wrong. It looks as if I am trying to print the address of
    structure itself instead of the member.


    Thanx in advance for any help ....
    , Jun 21, 2005
    #1
    1. Advertising

  2. writes:
    > I am trying to print the offset of a particulat member in
    > a structure, but it's not working.


    Use the offsetof() macro, defined in <stddef.h>. That's what it's
    for.

    > I am using the following
    > expression to print the offset.
    >
    > &(struct my_struct *)0->member_name
    >
    > What's wrong with this ?


    The -> operator binds more tightly than a cast, so you're trying to
    cast the result of 0->member_name.

    > If I use,
    > &((struct my_struct *)0)->member_name, it works. But it seems
    > to be wrong. It looks as if I am trying to print the address of
    > structure itself instead of the member.


    One possible definition of offsetof() is:

    #define offsetof(s, m) (size_t)(&(((s *)0)->m))

    (s *)0 is a pointer to a struct at address zero.

    ((s *)0)->m is a member of that structure.

    If the member is at offset 6 within the structure, that member of
    the structure at address 0 will have an address of 6.

    Converting that address to size_t yields the value 6, which is what
    you want.

    *All* of this is extremely system-specific. It depends on numerous
    assumptions about how address are represented, conversions from
    addresses to size_t, and so forth. You cannot make assumptions in
    portable code -- but the implementer of the runtime library is
    entitled to make whatever assumptions are supported by the compiler.

    That's why the offsetof() macro is defined in the standard library;
    there's no portable way to implement it, but there's always a
    non-portable way that works for a given implementation. The implementer
    is allowed to do things that you aren't.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jun 21, 2005
    #2
    1. Advertising

  3. Richard Bos Guest

    wrote:

    > I am trying to print the offset of a particulat member in
    > a structure, but it's not working. I am using the following
    > expression to print the offset.
    >
    > &(struct my_struct *)0->member_name
    >
    > What's wrong with this ?


    It doesn't use the macro the Standard provides for this very purpose,
    that's what's wrong with it. Also, you have failed to read the FAQ.
    Again.

    Richard
    Richard Bos, Jun 21, 2005
    #3
  4. CBFalconer Guest

    wrote:
    >
    > I am trying to print the offset of a particulat member in
    > a structure, but it's not working. I am using the following
    > expression to print the offset.
    >
    > &(struct my_struct *)0->member_name
    >
    > What's wrong with this ?


    It has nothing to do with reality. However there is a standard
    macro, named offsetof, to satisfy your urge.

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
    CBFalconer, Jun 21, 2005
    #4
  5. Rajan Guest

    This was what I did to get the offset:
    #define OFF(M,S) ((size_t)&(((M *)0)->S))

    typedef struct{
    int i, j;
    char c;
    }gen;

    I wanted to print offset of c;

    size_t size = OFF(gen,c);
    printf("%d\n",size);
    Try this out.

    BTW, I did'nt know there is offsetof which Keith pointed out.

    Do tell me if it works
    Rajan, Jun 21, 2005
    #5
  6. On Mon, 20 Jun 2005 23:33:26 -0700, junky_fellow wrote:

    > I am trying to print the offset of a particulat member in
    > a structure, but it's not working. I am using the following
    > expression to print the offset.
    >
    > &(struct my_struct *)0->member_name


    Standard C providea a macro to do this called offsetof() which is defined
    in the standard header <stddef.h>

    > What's wrong with this ?


    -> has higher precedence than a cast so it is parsed as
    &(struct mystruct *) (0->member_name)

    > If I use,
    > &((struct my_struct *)0)->member_name, it works. But it seems to be
    > wrong. It looks as if I am trying to print the address of structure
    > itself instead of the member.


    Ys it is generating the address of a structure element for a notional
    structure at address 0. You need to convert thjis to an integer to get an
    offsdet value. Of course this is non-portable. There is no way to write
    portable code to do this directly, so C defines a macro provided by each
    implementation which works on that implementations.

    Lawrence
    Lawrence Kirby, Jun 21, 2005
    #6
  7. Guest

    Keith Thompson wrote:
    > writes:
    > > I am trying to print the offset of a particulat member in
    > > a structure, but it's not working.

    >
    > Use the offsetof() macro, defined in <stddef.h>. That's what it's
    > for.
    >
    > > I am using the following
    > > expression to print the offset.
    > >
    > > &(struct my_struct *)0->member_name
    > >
    > > What's wrong with this ?

    >
    > The -> operator binds more tightly than a cast, so you're trying to
    > cast the result of 0->member_name.
    >


    Thanx a lot for the explanation. Actually, I was confused by the fact
    that ()
    operator has higher precedence than -> operator.
    But, here () have been used as a part of cast operator.
    , Jun 21, 2005
    #7
  8. On Tue, 21 Jun 2005 04:26:13 -0700, junky_fellow wrote:

    ....

    > Thanx a lot for the explanation. Actually, I was confused by the fact
    > that ()
    > operator has higher precedence than -> operator.


    The () in precedence tables refers to the function call operator
    which has the same precedence as ->. If it was specified with higher
    precedence then for example

    a.b();

    would be parsed incorrectly.

    Lawrence
    Lawrence Kirby, Jun 21, 2005
    #8
  9. Jack Klein Guest

    On 21 Jun 2005 02:16:11 -0700, "Rajan" <> wrote in
    comp.lang.c:

    > This was what I did to get the offset:
    > #define OFF(M,S) ((size_t)&(((M *)0)->S))


    Dereferencing a null pointer is undefined behavior.

    > typedef struct{
    > int i, j;
    > char c;
    > }gen;
    >
    > I wanted to print offset of c;
    >
    > size_t size = OFF(gen,c);
    > printf("%d\n",size);


    Passing an argument of type size_t to printf() with a "%d" conversion
    specifier produces undefined behavior.

    > Try this out.
    >
    > BTW, I did'nt know there is offsetof which Keith pointed out.


    Have you tried looking it up in your C reference book or copy of the
    standard? Or in your compiler's documentation, online help, or man
    pages?

    > Do tell me if it works


    Did you type:

    offsetof macro

    ....into the search box at google.com and look at the first few
    answers? You don't even need to open the linked articles, several of
    them answer your question in the small excerpt on the results page.

    Did you read question 2.14 in the FAQ for this newsgroup (link in my
    signature)? And its answer, of course.

    Note that the offsetof macro produces a value of type size_t, you need
    to cast it to int to pass to printf() with a "%d" conversion
    specifier.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 22, 2005
    #9
  10. Rajan Guest

    Jack,
    I tried doing this and it gives me absolutely no problems printing the
    offset.
    Check this out.
    Rajan, Jun 23, 2005
    #10
  11. "Rajan" <> writes:
    > Jack,
    > I tried doing this and it gives me absolutely no problems printing the
    > offset.
    > Check this out.


    You tried what? Check what out? Please provide some context; don't
    assume that everyone can easily see the article to which you're
    replying.

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    In your previous article, you said you used the following to compute
    the offset of a struct member:

    #define OFF(M,S) ((size_t)&(((M *)0)->S))

    As Jack explained, this invokes undefined behavior. Undefined
    behavior includes working as you think it should. The OFF macro is
    not-portable.

    There is a perfectly portable way to compute the offset of a struct
    member: the offsetof() macro defined in <stddef.h>.

    The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>. Question
    2.14 talks about the offsetof() macro. It also mentions an alternate
    method to be used if the offsetof() macro is not available, but that's
    largely obsolete; unless you're using a very old system, the
    offsetof() macro *will* be available.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jun 23, 2005
    #11
    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. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    485
    Patrick TJ McPhee
    Oct 15, 2003
  2. junky_fellow

    offset of a member inside a structure

    junky_fellow, Oct 14, 2004, in forum: C Programming
    Replies:
    15
    Views:
    972
    Tim Rentsch
    Oct 24, 2004
  3. gokrix

    Structure member offset.

    gokrix, Dec 2, 2005, in forum: C Programming
    Replies:
    10
    Views:
    619
    Richard Tobin
    Dec 2, 2005
  4. RAKHE

    OFFSET of Structure Member

    RAKHE, Jun 24, 2010, in forum: C Programming
    Replies:
    8
    Views:
    3,404
  5. Roy Smith
    Replies:
    4
    Views:
    240
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page