OFFSET of Structure Member

Discussion in 'C Programming' started by RAKHE, Jun 24, 2010.

  1. RAKHE

    RAKHE Guest

    struct emp
    {
    int a;
    char b;
    int c;
    };



    I want to find offset of the structure member a, b, c, please anyone
    explain about this
    RAKHE, Jun 24, 2010
    #1
    1. Advertising

  2. RAKHE wrote:
    > struct emp
    > {
    > int a;
    > char b;
    > int c;
    > };
    >
    > I want to find offset of the structure member a, b, c, please anyone
    > explain about this


    http://c-faq.com/struct/offsetof.html
    Alexander Klauer, Jun 24, 2010
    #2
    1. Advertising

  3. RAKHE

    Mark Bluemel Guest

    On 24 June, 11:07, RAKHE <> wrote:
    > struct emp
    >         {
    >                 int a;
    >                 char b;
    >                 int c;
    >         };
    >
    > I want to find offset of the structure member a, b, c, please anyone
    > explain about this


    What do you need explained?
    Mark Bluemel, Jun 24, 2010
    #3
  4. RAKHE <> writes:
    > struct emp
    > {
    > int a;
    > char b;
    > int c;
    > };
    >
    >
    >
    > I want to find offset of the structure member a, b, c, please anyone
    > explain about this


    Why? If you have the offset, what do you intend to use it for?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 24, 2010
    #4
  5. RAKHE

    Uno Guest

    pete wrote:

    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    > #include <stddef.h>
    >
    > int main(void)
    > {
    > struct emp {
    > int a;
    > char b;
    > int c;
    > };
    >
    > printf("offsetof(struct emp, a) is %lu\n",
    > (long unsigned) offsetof(struct emp, a));
    >
    > printf("offsetof(struct emp, b) is %lu\n",
    > (long unsigned) offsetof(struct emp, b));
    >
    > printf("offsetof(struct emp, c) is %lu\n",
    > (long unsigned) offsetof(struct emp, c));
    >
    > return 0;
    > }
    >
    > /* END new.c */
    >


    $ gcc -Wall -Wextra p2.c -o out
    $ ./out
    offsetof(struct emp, a) is 0
    offsetof(struct emp, b) is 4
    offsetof(struct emp, c) is 8
    $

    In this case, my implementation's macro is essentially the same as
    Plauger's.

    Linux:
    #ifdef __compiler_offsetof
    #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
    #else
    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
    #endif

    Plauger:
    #define offsetof(T, member) ((_Sizet)&((T *)0)->member)

    Can you say a few words about how it works?
    --
    Uno
    Uno, Jun 25, 2010
    #5
  6. Uno <> wrote:
    > Linux:
    > ...
    > #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
    > ...
    > Plauger:
    > #define offsetof(T, member)     ((_Sizet)&((T *)0)->member)
    >
    > Can you say a few words about how it works?


    <http://c-faq.com/struct/offsetof.html>

    The two implementations above assume null pointers map to
    'address zero'.

    --
    Peter
    Peter Nilsson, Jun 25, 2010
    #6
  7. RAKHE

    Uno Guest

    Peter Nilsson wrote:
    > Uno <> wrote:
    >> Linux:
    >> ...
    >> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


    [snipping Plauger.]
    [Please refer to the above macro herewith.]
    >> Can you say a few words about how it works?

    >
    > <http://c-faq.com/struct/offsetof.html>
    >
    > The two implementations above assume null pointers map to
    > 'address zero'.


    Can you "talk through" the whole thing? It takes me a bit with the logic.
    --
    UNo
    Uno, Jun 25, 2010
    #7
  8. On Jun 25, 2:22 pm, Uno <> wrote:
    > Peter Nilsson wrote:
    > > Uno <> wrote:
    > >> Linux:
    > >> ...
    > >> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    >
    > [snipping Plauger.]
    > [Please refer to the above macro herewith.]
    >
    > Can you "talk through" the whole thing?


    Suppose you have...

    struct {
    T1 elm1;
    T2 elm2;
    T3 elm3;
    };

    An instance will be layed out as...

    [elm1][elm2][elm3]
    ^ ^ ^
    | | |
    addr1 addr2 addr3

    ....where addr2 and addr3 will be a fixed number of bytes
    (offset) from addr1.

    Now suppose you could lay out the same instance at
    address 0...

    [elm1][elm2][elm3]
    ^ ^ ^
    | | |
    0 addr2 addr3

    At a guess, what do you suppose addr2 and addr3 will
    represent in the second case?

    --
    Peter
    Peter Nilsson, Jun 25, 2010
    #8
  9. RAKHE

    Uno Guest

    On 6/24/2010 10:25 PM, Peter Nilsson wrote:
    > On Jun 25, 2:22 pm, Uno<> wrote:
    >> Peter Nilsson wrote:
    >>> Uno<> wrote:
    >>>> Linux:
    >>>> ...
    >>>> #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)

    >>
    >> [snipping Plauger.]
    >> [Please refer to the above macro herewith.]
    >>
    >> Can you "talk through" the whole thing?

    >
    > Suppose you have...
    >
    > struct {
    > T1 elm1;
    > T2 elm2;
    > T3 elm3;
    > };
    >
    > An instance will be layed out as...
    >
    > [elm1][elm2][elm3]
    > ^ ^ ^
    > | | |
    > addr1 addr2 addr3
    >
    > ...where addr2 and addr3 will be a fixed number of bytes
    > (offset) from addr1.
    >
    > Now suppose you could lay out the same instance at
    > address 0...
    >
    > [elm1][elm2][elm3]
    > ^ ^ ^
    > | | |
    > 0 addr2 addr3
    >
    > At a guess, what do you suppose addr2 and addr3 will
    > represent in the second case?


    They are where struct.elm2 and struct.elm3 respectively begin.
    --
    Uno
    Uno, Jun 29, 2010
    #9
    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:
    498
    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:
    983
    Tim Rentsch
    Oct 24, 2004
  3. Replies:
    10
    Views:
    1,108
    Keith Thompson
    Jun 23, 2005
  4. gokrix

    Structure member offset.

    gokrix, Dec 2, 2005, in forum: C Programming
    Replies:
    10
    Views:
    625
    Richard Tobin
    Dec 2, 2005
  5. Roy Smith
    Replies:
    4
    Views:
    263
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page