please explain this macro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TYF
    New Member
    • Nov 2007
    • 11

    #1

    please explain this macro

    hi,
    can any one explain me what this little macro is doing?
    Code:
    #define pos_of(h, f)	((short int) &((h *) 0)->f)
    h is a struct
    f is a member of it

    i have no clue, i only know it has something to do with the position.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by TYF
    hi,
    can any one explain me what this little macro is doing?
    Code:
    #define pos_of(h, f)	((short int) &((h *) 0)->f)
    h is a struct
    f is a member of it

    i have no clue, i only know it has something to do with the position.
    Suppose you have a struct of type 'h' somewhere in memory, say starting at
    address 'a'. Then the member 'f' is located somewhere in memory at location,
    say 'a+x'. That macro finds that number 'x'.

    If you have a pointer to such a struct 's' you could've done this:

    [code=c]
    short int pos_of= ((char*)&(s->f))-((char*)&s);
    [/code]

    This macro doesn't assume that you have an actual struct in memory somewhere,
    so it supposes that you have a struct at location 0 (there is none, but as long
    as you don't dereference that address all is fine).

    So if that imaginary struct is located at 0 (((char*)&s) == 0), then the position
    of f of that imaginary struct is at position ((short int)&((h*)0)->f))
    (I'm sure I goofed with the parentheses here ;-)

    kind regards,

    Jos

    Comment

    • TYF
      New Member
      • Nov 2007
      • 11

      #3
      thanks for the explain, jos. this helps me alot! its still a little cryptic for me, but i think i understand most of it now. i will try it out and play with it later on, cause i need to do some app testing now.

      Comment

      Working...