Macro-sation error



  • Why isn't this code fragment compiled?

    #define container_of(ptr, type, field_name) ( \
    (type *) ((char *) (ptr) - offsetof(type, field_name)))
    

    Makes a mistake.

    C:\list.h:37: error: expected expression before ')' token (type *)((char *) (ptr) - offsetof(type, field_name)) ^



  • Try this one.

    #define container_of(field_addr,type,field_name)    \
      ((type *)((char *)field_addr -                    \
                (char *)&((type *)0)->field_name))
    

    I'm working.


    After the pastebine came up, everything came to the spot.

    Both macroes are working, but you have to compil. gcc -std=gnu99 ... No. -std=c99as the operator typeof() is the extension of GNU and is not accepted in C99.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2