container_of 有的情况下,只知道 struct结构中莫个成员的指针,而需要知道整个struct的指针 (如网卡驱动里面,list)
struct DDD {
        int a;
        int b;
        int c;
        int d;};struct DDD  ddd;
|------------|  <-------  得到这个
|      a         |
|------------|
|      b         |
|------------|
|      c         |  <-------  已知这个
|------------|
|      d         |
|------------|

//得到成员在结构
#define offsetof(TYPE, MEMBER) ((unsigned int) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); }) int main()
{
printf("ddd is %p and ddd.c is %p \r\n",&ddd,&ddd.c);
struct DDD * p= container_of(&ddd.c,struct DDD,c);
printf("calc p is %p \r\n",p);
return 0;
}
/*
output:
ddd is 0x601030 and ddd.c is 0x601038
calc p is 0x601030
结果与想象
*/

container_of用法及实现的更多相关文章

  1. c&c++中的宏

    1 c&c++中的宏 do {...} while (0); offsetof & container_of 2 引用 [1] do {...} while (0) 在宏定义中的作用 ...

  2. container_of 的用法

    1.问题:如何通过结构中的某个变量获取结构本身的指针???关于container_of见kernel.h中:/*** container_of - cast a member of a structu ...

  3. 刨一刨内核container_of()的设计精髓

    新年第一帖,总得拿出点干货才行,虽然这篇水分还是有点大,大家可以晒干了温水冲服.这段时间一直在整理内核学习的基础知识点,期间又碰到了container_of()这个宏,当然还包括一个叫做offseto ...

  4. container_of

    在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - cast ...

  5. container_of分析【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299589 1.container_of宏 1> Container_of在Linux内核 ...

  6. 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of

    4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...

  7. container_of分析--可用good【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299589 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.container_of宏 ...

  8. 关于container_of和list_for_each_entry 及其相关函数的分析

    Linux代码看的比较多了,经常会遇到container_of和list_for_each_entry,特别是 list_for_each_entry比较多,因为Linux经常用到链表,虽然知道这些函 ...

  9. (linux)container_of()宏

      在学习Linux驱动的过程中,遇到一个宏叫做container_of. 该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /**  * container_of - ...

随机推荐

  1. Vmware中Ubuntu挂ISO到虚拟光驱

    Ubuntu 下挂ISO到虚拟光驱的方法 https://i.cnblogs.com/EditPosts.aspx?opt=1 如果要ubuntu找到rpm软件包,需要把iso挂载到/media/cd ...

  2. (Problem 35)Circular primes

    The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, ...

  3. 移动平台WEB前端开发技巧汇总(转)

    最近我很关注移动前端的知识,但做为一个UI设计师和web前端工作人员没有这个工作环境接触,做为门外汉,网上系统的知识也了了,一直有种雾里看花的感觉,见到本文,我自己是奉为经典.所以我分享之后又专门打笔 ...

  4. cocos2dx进阶学习之CCTMXLayer

    继承关系 CCTMXLayer -> CCSpriteBatchNode CCTMXLayer是在瓦片地图中,抽象一个层的类,它继承自CCSpriteBatchNode,也就是说它抽象了一批相同 ...

  5. FileUtil

    package com.wiseweb.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInp ...

  6. Web API 设计摘要

    近期读了一本微电子书 Brian Mulloy 所著<Web API Design>感觉颇多收获,特对其内容做了个整理摘要以便回想其观点精华以指导日常工作中的设计思路. 本文主要讲述 We ...

  7. c#关于委托和事件

    using System; using System.Collections.Generic; using System.Text; namespace Delegate {     // 热水器   ...

  8. 计算阶乘并显示_winform (20以后的阶乘溢出)

    编写一个窗体应用程序,计算n的阶乘,显示其结果,同时,将结果显示在一个标签中. 新建窗体应用程序(如下),新建控件label1,label2,label3,textBOX1,button1,butto ...

  9. MFC、C++ 、Windows编程高手

    cnblogs: DoubleLi 1. DoubleLi  白手起家Win32SDK应用程序 http://www.cnblogs.com/lidabo/p/3450178.html#_Toc309 ...

  10. [Swust OJ 781]--牛喝水

    Time limit(ms): 1000 Memory limit(kb): 65535   The cows have a line of 20 water bowls from which the ...