Question on macro

Discussion in 'C Programming' started by arunaling@yahoo.co.in, Mar 30, 2007.

  1. Guest

    Hi all,

    Can I write some macro which does something like this offset(s,x),
    where s is the type of the structure and x is the name of the field in
    the structure. offset has to return the position of the element in the
    structure of type s.

    For eg., if the structure is declared as struct s {int x, int y}. If
    you give offset(s,x) return value should be 0. If you give
    offset(s,y), return value should be 2. Is it possible to write such a
    macro.

    Thanks in advance.

    Thanks and Regards,
    Aruna
    , Mar 30, 2007
    #1
    1. Advertising

  2. Chris Dollin Guest

    wrote:

    > Hi all,
    >
    > Can I write some macro which does something like this offset(s,x),
    > where s is the type of the structure and x is the name of the field in
    > the structure. offset has to return the position of the element in the
    > structure of type s.
    >
    > For eg., if the structure is declared as struct s {int x, int y}. If
    > you give offset(s,x) return value should be 0. If you give
    > offset(s,y), return value should be 2. Is it possible to write such a
    > macro.


    Yes, and in fact it's extremely easy:

    #include <stddef.h>

    #define offset(s,f) offsetof(s, f)

    `offsetof` is provided by C for exactly this purpose, because there
    is /no portable way/ of defining it oneself. The insides are magic
    and are only guaranteed to work on the implementation that provides
    it.

    --
    Of Sets And Hedgehogs
    "We did not have time to find out everything we wanted to know."
    - James Blish, /A Clash of Cymbals/
    Chris Dollin, Mar 30, 2007
    #2
    1. Advertising

  3. SM Ryan Guest

    wrote:
    # Hi all,
    #
    # Can I write some macro which does something like this offset(s,x),
    # where s is the type of the structure and x is the name of the field in
    # the structure. offset has to return the position of the element in the
    # structure of type s.

    It's easy to write a macro that will create an expression whose value
    is the field offset; it's unlikely you can make evaluate that in the
    preprocessor: it requires information from the context sensitive parse.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Don't say anything. Especially you.
    SM Ryan, Mar 30, 2007
    #3
  4. Eric Sosman Guest

    wrote:
    > Hi all,
    >
    > Can I write some macro which does something like this offset(s,x),
    > where s is the type of the structure and x is the name of the field in
    > the structure. offset has to return the position of the element in the
    > structure of type s.


    Easy!

    #include <stddef.h>
    #define offset(s,x) offsetof(s,x)

    > For eg., if the structure is declared as struct s {int x, int y}. If
    > you give offset(s,x) return value should be 0. If you give
    > offset(s,y), return value should be 2. Is it possible to write such a
    > macro.


    A different requirement, but also easy!

    #include <stddef.h>
    #define offset(s,x) (offsetof(s,x) ? 2 : 0)

    --
    Eric Sosman
    lid
    Eric Sosman, Mar 30, 2007
    #4
  5. Army1987 Guest

    <> ha scritto nel messaggio
    news:...

    > For eg., if the structure is declared as struct s {int x, int y}. If
    > you give offset(s,x) return value should be 0. If you give
    > offset(s,y), return value should be 2. Is it possible to write such a
    > macro.


    Do you know for certain that sizeof(int) == 2 and that there is no padding?
    Army1987, Mar 30, 2007
    #5
    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,091
    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:
    564
    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:
    486
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    458
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

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

Share This Page