当前位置:网站首页>使用共用体union及指针测试大小端
使用共用体union及指针测试大小端
2022-07-28 10:32:00 【tutu-hu】
1.共用体
一句话解释共用体就是:对同一片内存空间的不同解析方式。
2.大小端模式
大端模式:在内存中高地址存放低字节,低地址存放高字节
小端模式:在内存中高地址存放高字节,低地址存放低字节
3.代码测试大小端
(1)共用体方式
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//共用体判断大小端
union myunion
{
char a;
int c;
};
int main(int argc,char *argv[])
{
/* 使用共用体测试大小端 */
union myunion union1;
printf("sizeof(myunion) = %d\n",sizeof(union myunion)); //获取共用体大小
printf("&union1.a = %p\n",&union1.a); //打印a的地址
printf("&union1.c = %p\n",&union1.c); //打印c的地址
union1.c = 0x12345678;
if(union1.a == 0x78)
{
printf("小端模式\n");
}
else if(union1.a == 0x12)
{
printf("大端模式\n");
}
return 0;
}
(2)指针方式
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//指针方式判断大小端
int main(int argc,char *argv[])
{
/* 使用指针测试大小端 */
int a = 0x12345678;
if(*(char *)&a == 0x78)
{
printf("小端模式\n");
}
else if(*(char *)&a == 0x12)
{
printf("大端模式\n");
}
return 0;
}
4.总结
共用体本质上就是操作内存的方式,因此从本质上来说两者没有任何区别,测试结果完全一致。
边栏推荐
- Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter
- Aike AI frontier promotion (7.28)
- Lucene query syntax memo
- An example of SQL trace in MySQL
- PyQt5快速开发与实战 4.11 拖曳与剪贴板
- nodejs:搭建express 服务,设置session以及实现退出操作
- clo*******e:项目管理随记
- Sleeping barber problem
- Using k-means clustering to classify tariff models of different industries
- 11_ue4进阶_男性角色换成女性角色,并修改动画
猜你喜欢

蓝桥杯嵌入式-HAL库-LCD

GKCylindersNoiseSource

Crm+ zero code: easily realize enterprise informatization

GKSpheresNoiseSource

Product side data analysis thinking

PyQt5快速开发与实战 4.12 日历与时间

Characteristics and installation of non relational database mongodb

Blue Bridge Cup embedded Hal library USART_ RX

6. MapReduce custom partition implementation

GKBillowNoiseSource
随机推荐
Batch Normlization
两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6
Using k-means clustering to classify tariff models of different industries
Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market
c语言进阶篇:指针(一)
19. Delete the penultimate node of the linked list
PyQt5快速开发与实战 4.12 日历与时间
GKNoiseSource
The future of generating confrontation networks in deepfake
GKRidgedNoiseSource
判断数码管是共阳极还是共阴极
一文学会如何做电商数据分析(附运营分析指标框架)
Redis-day01 common sense supplement and redis introduction
Yan reported an error: could not find any valid local directory for nmprivate/
粒子群解决tsp的技术问题
JSON preliminary understanding
nodejs:检测并安装npm模块,如果已安装则跳过
零代码 | 轻松实现数据仓库建模,搭建BI看板
GKPerlinNoiseSource
The 10th Landbridge cup embedded electronic provincial competition