当前位置:网站首页>Understanding of union union (continued from the previous article data type float)
Understanding of union union (continued from the previous article data type float)
2022-08-07 00:18:00 【water love dirty】
目录
一、问题来源
在学习STM32读取数据,I stumbled across a line of code while doing data storage,Roughly the following values are different but the structure is the same
......
union data_type
{
unsigned char data[4];
float value;
} ;
float num;
union data_type value2;
......
......
......
value2.data[0]=0x78;
value2.data[1]=0x56;
value2.data[2]=0x34;
value2.data[3]=0x12;
num=value2.value;
......
When I haven't seen the specific data definition at the beginning,As soon as I saw the form of something, the first thing that came to my mind wasvalue2是一个结构体,One of the member variables is in the form of an array and stores some data,when looking further down,我在想这个numHow to assign an uninitialized structure member variable??
Then come back tovalue2是一个联合体,Only have this article and the previous article.
二、unionUnderstanding of the Commonwealth
What is a consortium,区别于结构体,A structure is a series of data collections with the same type or different types of data,是一些元素的集合,这些元素称为结构体的成员且这些成员可以为不同的类型,成员一般用名字访问.例如年龄、身高、Body weight constitutes a member of Zhang's triple structure,It is very convenient to call member name access,例如张三.年龄、张三.身高、张三.体重.
Unions, like structures, have many member variables defined in them.,At the same time, the storage space of structure member variables is independent of each other,The first address of each member variable of the union is the same and shares a storage space,举个例子:
typedef struct DATA_TYPE
{
unsigned char data[4];
float value;
}datatype;
datatype value1;
VS
union data_type
{
unsigned char data[4];
float value;
} ;
union data_type value2;
value1是结构体变量,value2Is a complex variable.
假设value1.data[0]~[4]内存地址从0x00开始到0x03,Store four bytes consecutively4个内存地址,之后紧跟着value1.value是floatType variables also store four bytes,从0x04到0x07,This is how struct types are stored.
而value2Is a complex variable,假设value2.data[0]~[4]内存地址从0x10开始到0x13,而value2.valueIs four bytes is from0x10到0x13.This is the main difference between union and structure.
具体来看,I modify the structure member variable,value1.data[0]=0x12;value1.value=1.0;There is no connection between the two member variables,Physical addresses are stored separately,不会造成影响,while modifying the union,value2.data[0]=0x12;value2.value=1.0read it latervalue2.data[0]时,You will find that the original value has been changed,This is because the two member variables of the union share the same storage space.Specific to see the next section.
三、Validation and Application
下面就来验证floatHow the type data structure is stored,More than one article is derived2.03和0.25为例来验证.我们通过unionconsortium to verifyfloat数据类型的存储方式,Using the characteristics of the union to share the same data memory address will constitutefloatThe four-byte number of type data is finally formed as a floating-point value by assigning a value at the specified memory address,具体如下:
#include <stdio.h>
#include <windows.h>
float num;
union data_type
{
unsigned char tem[4];
float value;
} ;
int main()
{
union data_type value2;
value2.tem[0]=0x00;
value2.tem[1]=0x00;
value2.tem[2]=0x80; // 1 000 1 100
value2.tem[3]=0x3E; // 0011 1111
num= value2.value;
printf("%f\r\n",num);
return 0;
}
0x3E800000就是小数0.25在内存中的存储结构.
替换参数:
value2.tem[0]=0x85;
value2.tem[1]=0xEB;
value2.tem[2]=0x01; // 1 000 1 100
value2.tem[3]=0x40; // 0011 1111
0x4001EB85就是小数2.03在内存中的存储结构.

visible beforefloatAuthenticity of Typed Data Structures,Via this and previous articles,Deepen your understanding of data types and structures、Understanding of the Commonwealth.
Temporarily write here Wuhu~
边栏推荐
猜你喜欢

Understanding the Graph Attention Mechanism

leetcode 23. 合并K个升序链表

博士也会毕业吗?

积极防御体系进阶:《DevSecOps敏捷安全》

strcmp、strstr、memcpy、memmove等库函数的用法和模拟实现

Deploy an LVS-DR cluster

[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.4 Introduction to Anti-Aliasing

CV领域经典backbone模型小抄(2)

Vi learning (2) the common commands include move cursor/highlight the text/undo and the undo/delete/copy and paste the text/find replacement/insert 】

谈谈程序员如何具备领导力
随机推荐
How does the fragment get the click event of the activity
多项式——多项式牛顿迭代
数学英语题目理解模型记录(1)
惯性测量单元预积分原理与实现
Detailed explanation of C51 basic functions, interrupt functions and library functions
手撕 Android Framework底层面试题集合
月薪8万炫富后续来了,金融业“限薪令”重磅出击
结构体内存对齐详解
vi学习(3)【分屏命令】待完善
语言模型大串烧之变形金刚
用同花顺开户安全么
【Day_13 0509】Parameter analysis
中国银河证券开户安全吗
抖音 滑块验证方案 s_v_web_id 参数分析
【搜索专题】看完必会的BFS解决最小步数问题攻略
对比学习模型小抄(1)
创建文件 Demo1.txt 写入文本 hello 创建文件 Demo2.txt 写入文本 Neuedu 将两个文件内容 提取出来输出到 第三个文件 Test.txt 通过 文件与流方式实现
常用邮件服务器支持端口及加密方法实测
MySql操作之DML
php three-dimensional array merge and accumulate their values according to a key