c语言中offset和container_of宏的讲解

博客主要讲解了C语言中offset宏和container_of宏。offset宏通过将0地址强制转换为指定类型指针,访问结构成员并取其地址,最后转换为size_t类型。container_of宏先定义临时指针保存ptr值,再通过减去成员偏移量得到结构体首地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.offset宏的讲解

# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

1)((type *) 0) =》0地址强制转化为type类型的指针;
2) ((type *)0)->MEMBER =》 访问type结构中的MEMBER数据成员
3) &(((type *)0)->MEMBER) =》去除type结构中数据成员member的地址
4) (size_t)(&(((type *)0)->Member)) =》将结果转化为size_t类型

2.container_of宏的讲解

# define container_of(ptr, type, member) ({	\
	 typeof( ((type *)0)->member ) *__mptr = (ptr);  \
	 (type *)( (char *)__mptr - offsetof(type,member) );})

1)typeof( ((type *)0)->member ) *__mptr = (ptr);
首先定义了一个临时的数据类型(通过 typedef(((type *)0)->member)获得)与ptr相同的指针变量_mptr,然后用它来保存ptr的值
2)用(char *)__mptr减去member在结构体中的偏移量,得到的值就是整个结构体变量的首地址(整个宏的返回值就是这个首地址)。

eg)

#include <stdio.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define  container_of(ptr, type, member) ({                      \
                      const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
                       (type *)( (char *)__mptr - offsetof(type,member) );})
struct test_struct {
           int num;
          char ch;
          float f1;
  };
 int main(void)
  {
          struct test_struct *test_struct;
          struct test_struct init_struct ={12,'a',12.3};
          char *ptr_ch = &init_struct.ch;
          test_struct = container_of(ptr_ch,struct test_struct,ch);
          printf("test_struct->num =%d\n",test_struct->num);
          printf("test_struct->ch =%c\n",test_struct->ch);
          printf("test_struct->ch =%f\n",test_struct->f1);
          return 0;
  }
执行结果:
jibo@jibo-VirtualBox:~/cv_work/work/list/container_of $ ./main
test_struct->num =12
test_struct->ch =a
test_struct->ch =12.300000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值