container_of:
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
(1) (type *)0):将0强转为type类型的指针
(2)((type *)0)->member):该指针指向type中的某一成员member
(3)((type *)0)->member) * __mptr = (ptr);将外部的指针赋予临时变量__mptr
(4)typeof(((type *)0)->member) * __mptr;获取类型
(5)#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)//获取memeber相对于0的地址偏移,并转换为size_t的数据
(6)将mptr地址减去member相对于0的地址偏移
struct test{ int num; char ch; }t1={100,'c'}; char *pch=&t1.ch; struct test *ptt=container_of(pch,struct test,ch); printf("num=%d\n",ptt->num);