首页 > 其他 > 详细

#define container_of(ptr, type, member)

时间:2021-05-07 14:56:55      阅读:21      评论:0      收藏:0      [点我收藏+]

#define container_of(ptr, type, member)

/* include/linux/kernel.h */

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({              const typeof( ((type *)0)->member ) *__mptr = (ptr);        (type *)( (char *)__mptr - offsetof(type,member) );})

输入:
结构体一个成员的member地址ptr,
结构体的类型,
结构体一个成员member的名,

输出:
得到这个结构体变量的地址;

分析:
结构体类型是确定的,结构体成员 相对 结构体定义的起始地址 偏移是确定的;
知道了一个结构体成员的地址,可以根据偏移得到 结构体定义的起始地址;

Author: Yangkai Wang
wang_yangkai@163.com
Coding in 2021/05/07

#define container_of(ptr, type, member)

原文:https://www.cnblogs.com/WangYangkai/p/14738645.html

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