昭通网站建设兼职,百度移动权重,成都网站建设cdxwcx,生物科技公司网站模板下载在 C 语言中#xff0c;union#xff08;联合体#xff09; 是一种数据结构#xff0c;它允许多个成员共享相同的内存空间。换句话说#xff0c;联合体中的所有成员都存储在同一块内存区域#xff0c;不同的成员会占用相同的内存地址#xff0c;但在同一时刻只能保存一个…在 C 语言中union联合体 是一种数据结构它允许多个成员共享相同的内存空间。换句话说联合体中的所有成员都存储在同一块内存区域不同的成员会占用相同的内存地址但在同一时刻只能保存一个成员的值。
1. union 的定义和基本结构
定义 union 的语法与 struct 类似但在 union 中所有成员共享同一块内存区域而在 struct 中每个成员都有自己独立的内存空间。
union Data {int i;float f;char str[20];
};这个 union Data 包含三个成员i整型、f浮点型、str字符数组。在内存中i、f 和 str 是共享同一块存储空间的因此它们不能同时保存值。
2. union 的内存分配
在 union 中分配的内存大小等于最大成员的大小。例如
union Data {int i; // 占 4 字节float f; // 占 4 字节char str[20]; // 占 20 字节
};联合体 Data 的大小是 20 字节str[20] 是最大的成员所有成员都共享这 20 字节的内存。
3. 如何使用 union
你可以像结构体一样定义和访问 union 的成员。注意一次只能使用一个成员并且最后存入的值会覆盖之前的值。
#include stdio.h
#include string.hunion Data {int i;float f;char str[20];
};int main() {union Data data;data.i 10;printf(data.i : %d\n, data.i);data.f 220.5;printf(data.f : %f\n, data.f);strcpy(data.str, Hello);printf(data.str : %s\n, data.str);return 0;
}在这段代码中data.i 最先被赋值为 10然后 data.f 被赋值为 220.5最后 data.str 被赋值为 Hello。注意当 str 被赋值后之前的 i 和 f 的值都被覆盖。
4. union 的用途
union 通常用于节省内存尤其在硬件编程或底层系统开发中常用于处理以下场景
4.1 节省内存
如果你有多个数据类型但这些类型不会同时使用可以通过 union 节省内存。例如在嵌入式系统中数据存储空间非常有限你可以使用 union 存储多种不同的类型。
4.2 数据类型的重解释
union 允许你以不同的方式解释相同的内存。这在需要对数据进行不同类型的访问时非常有用。
例如当你想通过字节数组访问一个整数的字节内容时可以使用 union
union IntByte {int i;unsigned char bytes[4];
};int main() {union IntByte data;data.i 0x12345678;printf(Bytes: %x %x %x %x\n, data.bytes[0], data.bytes[1], data.bytes[2], data.bytes[3]);return 0;
}这段代码允许你查看整数 0x12345678 在内存中的字节表示。
4.3 硬件寄存器访问
在嵌入式系统中经常需要访问硬件寄存器寄存器通常可以用 union 来表示。硬件寄存器有时既可以按位访问也可以按字节或整块访问使用 union 可以同时处理这些不同的访问方式。
例如
union Register {uint32_t full;struct {uint8_t byte1;uint8_t byte2;uint8_t byte3;uint8_t byte4;};
};这段代码允许你通过 full 访问整个 32 位寄存器也可以分别访问每个 8 位字节。
4.4 变体数据结构
在一些协议栈或者文件格式中你可能需要存储多种类型的数据但在一次操作中只使用其中一种。例如一个网络协议包可能包含多个不同类型的字段你可以使用 union 来表示这些字段从而在解析时节省内存。
5. union 的局限性
同时只能存储一个值联合体中的所有成员共享相同的内存空间因此在任一时刻只能存储一个成员的值。如果你想同时使用多个值就需要考虑使用 struct。调试复杂性由于不同的成员共享同一片内存因此调试时可能不容易区分哪个成员当前有效。
6. 总结
union 是一种允许多个成员共享同一块内存的结构体常用于节省内存或需要以不同方式解释相同内存时。它在嵌入式系统、硬件编程和数据解析方面非常有用但要注意它一次只能保存一个有效的值并且在使用中需要小心以防止未定义的行为。 文章转载自: http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.pskjm.cn.gov.cn.pskjm.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.rgrdd.cn.gov.cn.rgrdd.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.dodoking.cn.gov.cn.dodoking.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.kxypt.cn.gov.cn.kxypt.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.krwzy.cn.gov.cn.krwzy.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn