当前位置:网站首页>C语言程序设计 | offsetof宏的讲解及其模拟实现
C语言程序设计 | offsetof宏的讲解及其模拟实现
2022-07-27 22:27:00 【回不去_从前了丶】
offsetof宏讲解
首先来看下在cpluscplus对于offsetof宏给出的定义

macro代表的是宏
其中参数有两个
- type代表的是类型
- member代表的成员
该宏的作用是用来求出结构体成员相对于首地址的偏移量。
使用的方法如下,代码实例:
struct S
{
char c1;
int i;
char c2;
};
#include <stdio.h>
#include <stddef.h>
int main()
{
struct S s = {
0};
printf("%d\n", offsetof(struct S, c1));
printf("%d\n", offsetof(struct S, i));
printf("%d\n", offsetof(struct S, c2));
return 0;
}
打印结果:

讲解:

模拟实现offsetof宏
在讲解中,我们可以看出,偏移量就是地址减去首地址的出来的一个数,那么我们假设地址为0,我们只需要取出地址,这个地址就是偏移量。
代码实例:
#include <stddef.h>
#include <stdio.h>
struct S
{
char c1;
int i;
char c2;
};
#define OFFSETOF(type, m_name) (size_t)&(((type*)0)->m_name)
int main()
{
struct S s = {
0};
printf("%d\n", OFFSETOF(struct S, c1));
printf("%d\n", OFFSETOF(struct S, i));
printf("%d\n", OFFSETOF(struct S, c2));
return 0;
}
讲解:
首先我们把地址设为0,那么就有:
#define OFFSETOF(type,m_name) ((type*)0)
这里呢就是地址设置为0,类型为type*类型的,注意这里的type只是进行替换,并不代表实际的类型
取出地址之后呢,我们就去找结构体的成员,那么就有:
#define OFFSETOF(type,m_name) (((type*)0)->m_name)
这里找到结构体的成员之后,我们再取出这个成员的地址,那么就有:
#define OFFSETOF(type,m_name) &(((type*)0)->m_name)
取出地址之后呢,我们再进行类型的强制转换。
#define OFFSETOF(type,m_name) (size_t)(&(((type*)0)->m_name))
打印结果:

边栏推荐
- SRv6初登场
- One year anniversary of creation, Chongba young Lang
- Build Release Blogs
- Rongyun IM & RTC capabilities on new sites
- 自用图床搭建教程
- 推荐系统-精排模型:xDeepFM
- Firefox 103, the Firefox browser, has been released to improve performance under high refresh rate displays
- 网络安全漏洞分析与漏洞复现
- "C language" deep entry rounding & four functions
- Database daily question --- day 22: last login
猜你喜欢

接口测试实战项目02:读懂接口测试文档,上手操练

一文读懂CMake

跨桌面端Web容器演进

110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe

Data analysis: disassembly method (details)
![Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]](/img/6c/d4a45981a7fc87f6a82a91017f8ce8.png)
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]

Recurrence of fastjson historical vulnerabilities

iperf安装与使用

Confused SCM matrix keys

Set data constructor
随机推荐
Focus on demand flow rather than idle people
Basic operations of MySQL database (I) --- Based on Database
[proteus simulation] 51 single chip microcomputer washing machine simulation control program
MySQL中的运算符
Data analysis: disassembly method (details)
Uniapp display rich text effect demo (organize)
DC motor winding parameters
map集合
Branch and loop sentence exercises
[BuildRelease Management]Parabuild
进程与进程调度
Network device hard core technology insider firewall and security gateway (VIII) virtualization artifact (middle)
110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe
The most detailed summary of common English terms in the chip industry (quick grasp of graphics and text)
【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)
Leetcode:1997. the first day after visiting all rooms [jump DP]
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
Redis transaction and optimistic lock
Database daily question --- day 22: last login
Recommend a Hongmeng instant messaging software "fruit chat", which is a bit awesome!!