当前位置:网站首页>C language programming | explanation and Simulation of offsetof macro
C language programming | explanation and Simulation of offsetof macro
2022-07-28 01:06:00 【Can't go back_ Once upon a time】
offsetof Macro explanation
First, let's take a look at cpluscplus about offsetof The definition given by macro

macro Represents macro
There are two parameters
- type It stands for type
- member Members representing
The function of this macro is to calculate the offset of the structure member relative to the first address .
The methods used are as follows , Code instance :
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;
}
Print the results :

Explain :

Simulation Implementation offsetof macro
In the explanation , We can see that , The offset is the number of the address minus the first address , Then let's assume the address is 0, We just need to take out the address , This address is the offset .
Code instance :
#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;
}
Explain :
First we set the address to 0, Then there is :
#define OFFSETOF(type,m_name) ((type*)0)
Here, the address is set to 0, The type is type* Type of , Notice the type It's just a replacement , Does not represent the actual type
After taking out the address , Let's find the members of the structure , Then there is :
#define OFFSETOF(type,m_name) (((type*)0)->m_name)
After finding the members of the structure here , Let's take out the address of this member , Then there is :
#define OFFSETOF(type,m_name) &(((type*)0)->m_name)
After taking out the address , Let's cast the type .
#define OFFSETOF(type,m_name) (size_t)(&(((type*)0)->m_name))
Print the results :

边栏推荐
- Operators in MySQL
- Border width border fillet border color
- Leetcode - find the median of two positively ordered arrays
- Rongyun IM & RTC capabilities on new sites
- Solve maze problem recursively
- LeetCode_ Bit operation_ Medium_ 137. Number II that appears only once
- "C language" deep entry rounding & four functions
- Process and process scheduling
- 论文赏析[ICLR18]联合句法和词汇学习的神经语言模型
- Basic operations of MySQL database (I) --- Based on Database
猜你喜欢

KMP review + AC automata Prequel

网络安全漏洞分析与漏洞复现
![论文赏析[ICLR18]联合句法和词汇学习的神经语言模型](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
![Jerry Zhi doesn't play hidden audio files [article]](/img/09/b9fb293151f56d2a93f8a1c8f3d0dc.png)
Jerry Zhi doesn't play hidden audio files [article]

How does JMeter solve the problem of garbled code?

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

Data analysis: disassembly method (details)

融云 IM & RTC 能力上新盘点

【STM32】看门狗模块
随机推荐
Shell系统学习之循环结构
scrollview、tableView嵌套解决方案
0-1背包问题
数组相关知识
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
Analysis and recurrence of network security vulnerabilities
进程与进程调度
Can TSMC Samsung build a production line without American equipment for Huawei?
c# 反射之Type使用
Set data constructor
LED, nixie tube and key of single chip microcomputer
Recommendation system model: wide & deep model
Network equipment hard core technology insider firewall and security gateway (V) security double repair method
Confused SCM matrix keys
Recommended system - offline recall: u2tag2i, ICF
Wavelet transform learning notes
R language uses ggplot2 visualization: use ggpattern package to add custom stripe patterns, shadows, stripes, or other patterns or textures to the grouped bar graph
Storage of deep planing data in memory
592. Fraction addition and subtraction: introduction to expression calculation
Syntaxerror resolved: positive argument follows keyword argument