博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 内核宏 container_of
阅读量:4074 次
发布时间:2019-05-25

本文共 965 字,大约阅读时间需要 3 分钟。

1.定义

container_of(ptr, type, member);ARGUMENTS       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.

作用:通过一个结构体变量中的成员地址找到这个结构体变量的首地址

2.例子

#include 
#undef offsetof#ifdef __compiler_offsetof#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)#else#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)#endif#define container_of(ptr, type, member) ({ \ const typeof( ((type*)0)->member ) *__mptr = (ptr); \ (type*)( (char*)__mptr - offsetof(type,member) ); })typedef struct { int age; int num;} student;int main(){ student s; printf("ss:%p\n", &s); student *temp = container_of(&s.num, student, num); printf("temp:%p\n", temp); return 0;}结果:ss:0x7fff156fe320temp:0x7fff156fe320通过s.num的地址找到了结构体变量的地址

转载地址:http://sxkni.baihongyu.com/

你可能感兴趣的文章
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
从mysql中 导出/导入表及数据
查看>>