如何通过结构体中的任意一个元素的地址得到这个结构体的首地址(container_of)

 

 

  #define container_of(ptr, type, member) ({             /
         const typeof( ((type *)0)->member ) *__mptr = (ptr);     /
         (type *)( (char *)__mptr - offsetof(type,member) );})

 

  #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

 

注意((type *)0)的使用。比如:((void *)0)();

((TYPE *)0)->MEMBER;--->这是指MEMBER在结构体TYPE中的偏移。

posted @ 2012-12-22 14:32  qimi  阅读(774)  评论(0编辑  收藏  举报