与struct相关的宏定义(成员变量偏移和获取struct起始地址)
来源:互联网 发布:我的世界ios怎么装js 编辑:程序博客网 时间:2024/05/16 18:45
学过C语言的人都知道,我们可以通过struct定义自己的数据集合,从而实现对数据的封装功能。
在实际的项目实践中,struct定义的结构随处可见。使用struct并不难,网上也有很多介绍struct的文章。
从如构定义到如何使用,讲得很详细。在此我就不多讲。
这里主要介绍在struct中如何获取成员变量的偏移以及通过成员变量的地址获取struct的起始地址。
第二个问题是紧密依赖第一个问题的。
1. 获取成员变量的偏移。
直接上代码吧:
#define offset_of(type, field) ( (unsigned int)&(((type *)(0))->field) )
这个宏对于刚开始学习C语言的人来说是比较困难的。
要理解这个宏,首先要理解宏定义到底是怎么一回事,还有struct是怎么一回事,还有指针的用法。
简单解释一下这个宏:
如果我们定义一个struct:
typedef struct{
int a;
int b;
} A;
那么offset_of(A, b)就会输出b相对于起始地址的偏移。
& -- 这是取地址符号,表示获取当前变量的地址。
0 -- 这是这个宏中最核心的地方。我们知道,对于一个struct, 比如说上面的A, &A->b代表了b的地址, 如果我们想得到b相对于A的偏移,那么&A->b - &A就可以得到。
但如是&A就是0呢。那么&A->b不就是偏移了吗?
好了,当我们有了获取成员变量相对偏移的宏。如果已知成员变量的地址,那我们也可以轻松得获取该成员所在结构体的首地址。
代码如下:
#define container_of(ptr, type, field) (type *)((char *)ptr - offset_of(type, field))我们只需要把该成员变量的地址减去你相对于首地址的偏移,不就是该结构体的首地址了么?
好了,不多说了,结出一个完整的实例:
// testConsole.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <time.h>#include <stdio.h>#include <stdlib.h>#define offset_of(type, field) ( (unsigned int)&(((type *)(0))->field) )#define container_of(ptr, type, field) (type *)((char *)ptr - offset_of(type, field))typedef struct{ int a; int b; char c; int d;}package_t;int main(){ package_t pkg; int *p; p = (int *)&pkg.d; printf("offset of 'd' is %d.\n", offset_of(package_t, d)); printf("The addr of pkg = 0x%x.\n", &pkg); printf("The addr of d's container = 0x%x.\n", container_of(p, package_t, d)); return 0;}
- 与struct相关的宏定义(成员变量偏移和获取struct起始地址)
- 与struct相关的宏定义 --- 根据成员变量偏移,获取struct起始地址
- 获取struct成员偏移量的方法
- 获取成员变量的地址和偏移地址
- 关于取struct成员的偏移
- 根据class/struct中一个成员的地址,获取该class/struct的地址
- struct成员变量
- 算出任意struct的某个成员的偏移量
- C++中struct成员的偏移量的计算
- 指定struct成员变量的位数
- UDP struct ifreq和struct ifconf获取IP地址
- 与struct相关的宏定义 ---今Tencent笔试用到的
- struct 不定义变量的使用方法
- typedef定义的struct类型与struct定义的结构体变量在使用上的区别
- 写宏定义:得到一个field在结构体(struct type)中的偏移量。
- 写宏定义:得到一个field在结构体(struct type)中的偏移量
- 写宏定义:得到一个field在结构体(struct type)中的偏移量[转]
- 宏定义:得到一个field在结构体(struct type)中的偏移量
- Python 的range和xrange
- 第三周上机任务项目3-时间类
- Python in SourceInsight
- C++的64位整数
- android 手机设备唯一标识
- 与struct相关的宏定义(成员变量偏移和获取struct起始地址)
- 反射机制
- C语言实现舞伴问题
- 关于魔方矩阵的心得
- 什么是java序列化,如何实现java序列化?
- 1004. Counting Leaves
- win xp(win7)+IE8访问oracle ebs 11i异常问题解决办法
- 如何把自己的驱动编译进内核或模块
- 启动apk方法