Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1292003
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: C/C++

2009-03-21 20:59:02

#include
#include

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

struct Substru
{
       int val;
};
          
struct Stru
{
       int val;
       struct Substru sub;
};

int main()
{
    struct Stru ptr;
    container_of(&ptr.sub, struct Stru, sub);
   
    printf("%x %x %x\n", container_of(&ptr.sub, struct Stru, sub), &ptr, &ptr.sub);
   
    while(1);
    return 0;
}
运行结果:

阅读(1733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~