首页 > 其他 > 详细

container_of

时间:2016-05-15 22:49:04      阅读:342      评论:0      收藏:0      [点我收藏+]
 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

 



container_of

原文:http://www.cnblogs.com/embedded-linux/p/5496462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!