container_of的使用
来源:互联网 发布:和禹网络培训费 编辑:程序博客网 时间:2024/06/02 16:10
contanier_of是Linux内核中常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体的地址
正确的使用如下:
#include <stdio.h>#include <string.h>#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) struct test_struct { int num; char ch; float fl; }; int main(void) { struct test_struct init_test_struct = { 99, 'C', 59.12 }; char *char_ptr = &init_test_struct.ch; struct test_struct *test_struct = container_of(char_ptr, struct test_struct, ch); printf(" test_struct->num = %d\n test_struct->ch = %c\n test_struct->fl = %f\n", test_struct->num, test_struct->ch, test_struct->fl); return 0; }
0 0
- container_of的使用
- container_of宏使用分析
- container_of使用分析
- linux中container_of(ptr, type, member)的使用
- C语言小结--offsetof和container_of宏的使用
- 对container_of的理解
- 对container_of的理解
- uclinux内核的container_of
- linux container_of 的用法
- container_of()宏的解析
- container_of的理解
- container_of的实现分析
- container_of的理解
- 神奇的container_of
- container_of的理解
- linux container_of的试验
- linux container_of 的用法
- 神奇的宏 container_of()
- C++到底还能做什么? ----感觉说的还是很有道理的,共勉!
- [leetcode] 237.Delete Node in a Linked List
- Java多线程的用法详解
- 用一条sql语句查出每个name最近的记录
- kafka manager安装
- container_of的使用
- mysql日志详细解析
- 01背包 HDU——2546 饭卡
- 常用的宏定义
- Terminal 命令
- android之官方下拉刷新组件SwipeRefreshLayout
- Swift 字典Dictionary
- 无向图的连通性分析
- RSA加密解密