计算结构体偏移量

方法一、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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值