中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
ptr:指向结构字段的指针。
type:包装指针的结构类型(即指针的类别)
member:ptr指向的结构内字段的名称(如结构体中的成员)
举个例子:
struct person{ int age; char *name; }somebody; struct person *the_person the_person = container_of (somebody.name, struct person, name);
最终结果the_person由container_of考虑到name从该结构开始处的偏移量,进而获取正确的指针位置。
微信扫码分享