设为首页 加入收藏

TOP

如何通过结构中的某个变量获取结构本身的指针?(一)
2014-11-23 23:18:13 来源: 作者: 【 】 浏览:1
Tags:如何 通过 结构 某个 变量 获取 本身 指针

问题:如何通过结构中的某个变量获取结构本身的指针???

关于container_of见kernel.h中:
/**
* container_of - cast a member of a structure out to the containing structure
* @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.
*
*/
#define container_of(ptr, type, member) ({ /
const typeof( ((type *)0)->member ) *__mptr = (ptr); /
(type *)( (char *)__mptr - offsetof(type,member) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.

关于offsetof见stddef.h中:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
TYPE是某struct的类型 0是一个假想TYPE类型struct,MEMBER是该struct中的一个成员. 由于该struct的基地址为0, MEMBER的地址就是该成员相对与struct头地址的偏移量.
关于typeof,这是gcc的C语言扩展保留字,用于声明变量类型.
const typeof( ((type *)0->member ) *__mptr = (ptr);意思是声明一个与member同一个类型的指针常量 *__mptr,并初始化为ptr.
(type *)( (char *)__mptr - offsetof(type,member) );意思是__mptr的地址减去member在该struct中的偏移量得到的地址, 再转换成type型指针. 该指针就是member的入口地址了.


例一;

container_of宏定义在[include/linux/kernel.h]中:

#define container_of(ptr, type, member) /

const typeof( ((type *)0)->member ) *__mptr = (ptr); /

(type *)( (char *)__mptr - offsetof(type,member) );

offsetof宏定义在[include/linux/stddef.h]中:

#define offsetof(type, member) ((size_t) &((type *)0)->member)
下面用一个测试程序test.c来说明

#include
struct student{
char name[20];
char sex;
}stu={"zhangsan",'m'};

main()
{
struct student *stu_ptr; //存储container_of宏的返回值
int offset; //存储offsetof宏的返回值
//下面三行代码等同于 container_of(&stu.sex,struct student, sex )参数带入的情形

const typeof(((struct student*)0)->sex) *_mptr = &stu.sex;
//首先定义一个 _mptr指针, 类型为struct student结构体中sex成员的类型
//typeof 为获取(((struct student*)0)->sex)的类型,此处此类型为char
//((struct student*)0)在offsetof处讲解

offset = (int)(&((struct student *)0)->sex);
/*((struct student*)0)为 把 0地址 强制转化为指向student结构体类型的指针
该指针从地址 0 开始的 21个字节用来存放name 与 sex(char name〔20〕与 char sex共21字节)
sex存放在第20个字节出(从0字节开始)
&((struct student *)0)->sex 取出sex地址(此处即为20) 并强制转化为整形
所以offset为20,后面的printf结果将证明这一点*/

stu_ptr = (struct student *)((char*)_mptr - offset);
/*((char*)_mptr - offset)此处先把_mptr指针转化为字符形指针
(为什么这么做呢? 如果_mptr为整形指针 _mptr - offset 相当于减去 sizeof(int)*offset个字节)
减去 offset值 相当于 得到_mptr所在结构体的首地址(即stu的地址)
然后我们把 该地址 强制转化为 struct student类型即可正常使用了*/

printf("offsetof stu.sex = %d/n",offset);
printf("stu_ptr->name:%s/tstu_ptr->sex:%c/n", stu_ptr->name, stu_ptr->sex);
return 0;
}

例二:

它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针。比如,有一个结构体变量,其定义如下:

1. struct demo_struct {
2. type1 member1;
3. type2 member2;
4. type3 member3;
5. type4 member4;
6. };
7.
8. struct demo_struct demo;

同时,在另一个地方,获得了变量demo中的某一个域成员变量的指针,比如:

1. type3 *memp = get_member_pointer_from_somewhere();

此时,如果需要获取指向整个结构体变量的指针,而不仅仅只是其某一个域成员变量的指针,我们就可以这么做:

1. struct demo_struct *demop = container_of(memp, struct demo_struct, member3);
首先,我们将container_of(memp, struct demo_struct, type3)根据宏的定义进行展开如下:

1. struct demo_struct *demop = ({ /
2. const typeof( ((struct demo_struct *)0)->member3 ) *__mptr = (memp); /
3. (struct demo_struct *)( (char *)__mptr - offsetof(struct demo_struct, member3) );})

其中,typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型。因此,上述代码中的第2行的作用是首先使用typeof获取结构体域变量member3的类型为 type3,然后定义了一个type3

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言计算随机数 下一篇libpcap抓包源码

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: