摘要:本文主要向大家介绍了C/C++知识点之container_of 和 offsetof宏,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之container_of 和 offsetof宏,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
在linux 驱动源码中,有两个很有意思的宏,分别是offsetof和container_of,他们的作用和定义是这样子的
offsetof 获得结构体某成员相对于结构体的偏移长度
/** * 计算的结构体中某成员的偏移量 */#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
这个实现很巧妙,相当于设结构体的整体地址设成0,然后它的成员的地址就相当于成员的偏移
测试代码:
#include <stdio.h>#include ""list.h""typedef struct Test_struct{ char ca; int ib; double dc; }Test_struct_t;int main(){ printf(""offset ca is %d\n"",offsetof(Test_struct_t,ca)); printf(""offset ib is %d\n"",offsetof(Test_struct_t,ib)); printf(""offset dc is %d\n"",offsetof(Test_struct_t,dc)); return 0; }
执行结果:
offset ca is 0
offset ib is 4
offset dc is 8
2.container_of 是通过结构体的某成员的地址得到整个结构体的地址,在使用上进而可以访问到结构体的其余成员
/** * 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) );})
实现上实际就是当前这个成员的地址 减去 这个成员相对整个结构体的的偏移量,就是结构体的地址
测试代码
typedef struct person{ char name[20]; char phone_num[12]; int index; }Person_t;void showInfo(Person_t *ptr){ printf(""name :%s\n"",ptr->name); printf(""phone_num :%s\n"",ptr->phone_num); printf(""index :%d\n"",ptr->index); }// 通过姓名打印所以信息void getInfoByName(char name[]){ if(!name) return ; Person_t *ptr = container_of(name,Person_t,name); if(ptr) { showInfo(ptr); } }int main(){ Person_t person; memset(&person,0x0,sizeof(Person_t)); person.index = 110; strncpy(person.name,""zhangsan"",strlen(""zhangsan"")); strncpy(person.phone_num,""15617274954"",strlen(""15617274954"")); showInfo(&person); printf(""\============\n""); getInfoByName(&person.name); return 0; }
执行结果
name :zhangsan
phone_num :15617274954
index :110
\============
name :zhangsan
phone_num :15617274954
index :110
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号