标签:
1 /** 2 * container_of - cast a member of a structure out to the containing structure 3 * @ptr: the pointer to the member. 4 * @type: the type of the container struct this is embedded in. 5 * @member: the name of the member within the struct. 6 * 7 */ 8 #define container_of(ptr, type, member) ({ 9 const typeof( ((type *)0)->member ) *__mptr = (ptr); 10 (type *)( (char *)__mptr - offsetof(type,member) );})
它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针。
1 #undef offsetof 2 #ifdef __compiler_offsetof 3 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) 4 #else 5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 6 #endif
标签:
原文地址:http://www.cnblogs.com/embedded-linux/p/5496462.html