方法一、c标准库 <stddef.h>
C 库宏 offsetof(type, member-designator) 会生成一个类型为 size_t 的整型常量,它是一个结构成员相对于结构开头的字节偏移量。成员是由 member-designator 给定的,结构的名称是在 type 中给定的。
下面是 offsetof() 宏的声明。
offsetof(type, member-designator)
参数
- type -- 这是一个 class 类型,其中,member-designator 是一个有效的成员指示器。
- member-designator -- 这是一个 class 类型的成员指示器。
返回值
该宏返回类型为 size_t 的值,表示 type 中成员的偏移量。
实例
下面的实例演示了 offsetof() 宏的用法。
#include <stddef.h>
#include <stdio.h>
struct address {
char name[50];
char street[50];
int phone;
};
int main()
{
printf("address 结构中的 name 偏移 = %d 字节。\n",
offsetof(struct address, name));
printf("address 结构中的 street 偏移 = %d 字节。\n",
offsetof(struct address, street));
printf("address 结构中的 phone 偏移 = %d 字节。\n",
offsetof(struct address, phone));
return(0);
}
让我们编译并运行上面的程序,这将产生以下结果:
address 结构中的 name 偏移 = 0 字节。
address 结构中的 street 偏移 = 50 字节。
address 结构中的 phone 偏移 = 100 字节。
方法二、不使用c标准库,自定义宏函数
typedef struct _st
{
int a;
char b[64];
char c;
}st;
st *p=NULL;
int off = -1;
分别计算各成员偏移量
off =(int)&(p->a);
printf("off_a=%d\n",off);//0
off =(int)&(p->b);
printf("off_b=%d\n",off);//4
off =(int)&(p->c);
printf("off_c=%d\n",off);//68
NULL其实就是0,可以将结构体类型st映射到内存地址0,于是p可以直接用0替换,并转换为p所指向的地址空间数据类型,指针必须指定类型
off =(int)&(((st*)0)->a);
printf("off_a=%d\n",off);//0
off =(int)&(((st*)0)->b);
printf("off_b=%d\n",off);//4
off =(int)&(((st*)0)->c);
printf("off_c=%d\n",off);//68
根据上面的推算,可以总结如下宏函数
#define OFFSETOF(TYPEST,MEMBER) ((size_t)&((TYPEST*)0)->MEMBER)
#define OFFSET(TYPEST,MEMBER) ((size_t)&((TYPEST*)0)->MEMBER)
参数一 是结构体类型
参数二 是需要结算偏移量的成员
typedef struct node
{
int a;
char b[64];
double c;
}node;
node* p=NULL;
node a={1,"abc",8.4};
int asizeoffset = OFFSET(node,a);//使用宏计算成员a偏移量
int bsizeoffset = OFFSET(node,b);//使用宏计算成员b偏移量
int csizeoffset = OFFSET(node,c);//使用宏计算成员c偏移量
printf("%d\n",asizeoffset);//0
printf("%d\n",bsizeoffset);//4
printf("%d\n",csizeoffset);//72
printf("a=%p\n",&a);//0xC6FAC0
printf("node.a=%p\n",(char*)&a+asizeoffset);//0xC6FAC0
printf("node.a=%p\n",&a.a);//0xC6FAC0
printf("node.b=%p\n",(char*)&a+bsizeoffset);//0xC6FAC4
printf("node.b=%p\n",&a.b);//0xC6FAC4
printf("node.c=%p\n",(char*)&a+csizeoffset);//0xC6FB08
printf("node.c=%p\n",&a.c);//0xC6FB08
根据偏移量取值
先将结构体变量起始地址转换成char* 类型,然后加上成员的偏移量,然后整体指针类型换为成员变量的类型即可。
printf("node.a=%d\n",*(int*)((char*)&a+asizeoffset));//1
printf("node.b=%s\n",*(char (*)[64])((char*)&a+bsizeoffset));//abc
printf("node.c=%f\n",*(double*)((char*)&a+csizeoffset));//8.400000