DistanceInStruct macro.

Discussion in 'C++' started by lallous, Jun 29, 2003.

  1. lallous

    lallous Guest

    I have the following:

    struct struc1
    {
    int a;
    int b;
    int c;
    };

    #define DistanceInStruct(member, struc) (unsigned long)((unsigned
    long)(&struc.member) - (unsigned long)(&struc))

    I can to this:

    int main()
    {
    struc1 A;

    int pos = DistanceInStruct(b, A); // returns 1 * sizeof(int)
    pos = DistanceInStruct(c, A); // returns 2 * sizeof(int)
    }

    how can i accomplish the same but w/o having to declare a local
    variable "A" of type 'struc1' ?

    Regards,
    Elias
    lallous, Jun 29, 2003
    #1
    1. Advertising

  2. "lallous" <> wrote in message
    news:...
    > I have the following:
    >
    > struct struc1
    > {
    > int a;
    > int b;
    > int c;
    > };
    >
    > #define DistanceInStruct(member, struc) (unsigned long)((unsigned
    > long)(&struc.member) - (unsigned long)(&struc))
    >
    > I can to this:
    >
    > int main()
    > {
    > struc1 A;
    >
    > int pos = DistanceInStruct(b, A); // returns 1 * sizeof(int)
    > pos = DistanceInStruct(c, A); // returns 2 * sizeof(int)
    > }
    >
    > how can i accomplish the same but w/o having to declare a local
    > variable "A" of type 'struc1' ?
    >
    > Regards,
    > Elias


    Use the offsetof macro, defined in <stddef.h>

    int pos = offsetof(struc1, b);

    john
    John Harrison, Jun 29, 2003
    #2
    1. Advertising

  3. lallous

    lallous Guest

    Hi John,

    Thanks that what I was looking for.

    Elias
    lallous, Jun 30, 2003
    #3
    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. Dead RAM
    Replies:
    20
    Views:
    1,097
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    568
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    488
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    462
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    441
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page