C语言 union联合体变量 计算问题 C语言中关于联合体中成员变量的问题

C\u8bed\u8a00 union\u8054\u5408\u4f53\u53d8\u91cf \u8ba1\u7b97\u95ee\u9898

\u4ec0\u4e48\u6570\u7ec4\u554a\uff01
\u4eba\u5bb6\u662f\u8054\u5408\u4f53\uff0c\u662fc\u91cc\u9762\u7684\u4e00\u79cd\u6570\u636e\u7c7b\u578b
\u697c\u4e3b\uff0c\u6211\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u4f60\u8fd9\u6837\u7684\u7528\u6cd5\u662f\u9519\u8bef\u7684\u3002
\u867d\u7136f9\u7684\u65f6\u5019\u6ca1\u6709\u62a5\u9519
\u4f46\u6211\u4ece\u6765\u6ca1\u6709\u89c1\u8fc7\u6709\u8fd9\u4e48\u7528\uff08\u4e5f\u53ef\u80fd\u662f\u6211\u5fd8\u4e86\u6216\u672c\u6765\u5c31\u89c1\u8bc6\u77ed\uff09
#include
main(){
union{
inta;
floatb;
charc;
}x;
x.a=5;
printf("\n%d",x.a);
x.b=105.005;
printf("\n%f",x.b);
x.c='a';
printf("\n%c",x.c);
}
/******\u539f\u6765\u697c\u4e0b\u7684****/
\u7531\u4e8e\u5728\u8054\u5408\u4f53\u4e2d\u7684\u5404\u53d8\u91cf\u5185\u5b58\u5730\u5740\u76f8\u540c\uff0c\u5927\u5c0f\u4e3a\u6700\u5927\u7684\u6700\u5927\u7c7b\u578b\u7684\u957f\u5ea6\u3002\u521d\u59cb\u53165\u7684\u6570\u636e\u76f4\u63a5\u50a8\u5b58\u4e0e\u8be5\u5730\u5740\u51fa\uff0c\u6309\u7167\u53d6\u503c\u7c7b\u578b\u8fdb\u884c\u5904\u7406\u3002\u7136\u800c\uff0c\u6570\u636e\u653e\u5165\u5185\u5b58\u4e2d\u7684\u5730\u5740\u662f\u4f4e\u5b57\u8282\u5728\u524d\u9762\uff0c\u800c\u9ad8\u5b57\u8282\u5728\u540e\u9762\uff0c\u6240\u6709printf("\n%d",x.c);\u4e5f\u8f93\u51fa5\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u8d85\u8fc7\u4e00\u4e2a\u5b57\u8282\u7684\u6570\uff0c\u5982258\uff0c\u5373
union{
inta;
floatb;
charc;
}x;
x.a=258;
printf("\n%d",x.c);
\u770b\u770b\uff0c\u7ed3\u679c\u5c31\u4e0d\u4e00\u81f4\u4e86\u3002
\u5728\u5b58\u653e\u6570\u636e\u7684\u65f6\u5019\u662f\u6309union\u91cc\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u5b58\u653e\u7684\uff0c\u8bfb\u53d6\u7684\u65f6\u5019\u4e5f\u662f\u6309union\u91cc\u5b9a\u4e49\u7684\u7c7b\u578b\u8fdb\u884c\u8bfb\u53d6\u7684\uff0c\u4f46\u8f93\u51fa\u7684\u8868\u793a\u5f62\u5f0f\u5374\u662f\u7531printf("\n%d",x.c);\u4e2d\u7684\uff08%d\uff09\u63a7\u5236\u7684\u3002\u6211\u4eec\u77e5\u9053char\u7c7b\u578b\u7684\u6570\u636e\u53ea\u5360\u4e00\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5\u8bfb\u51fa\u65f6\u4e5f\u53ea\u8bfb\u4e00\u4e2a\u5b57\u8282\u7684\u957f\u5ea6\uff0c\u5f53\u6240\u653e\u7684\u6570\u636e\u5927\u4e8e\u4e00\u4e2a\u5b57\u8282\u7684\u65f6\u5019\u5b83\u4f1a\u820d\u53bb\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\uff0c\u5728\u7528%d\u8f93\u51fa\u7684\u65f6\u5019\u5c31\u4f1a\u6bd4\u4f60\u539f\u6765\u7684int\u578b\u6570\u636e\u8981\u5c0f256\uff08\u4e00\u4e2a\u5b57\u8282\u80fd\u8868\u793a\u7684\u6700\u5927\u6574\u6570\uff09\u2014\u2014\u8fd9\u5c31\u662f\u7ed3\u679c\u4e0d\u4e00\u81f3\u7684\u539f\u56e0\u3002
\u8fd8\u6709\u4e00\u4e2a\u503c\u5f97\u91cd\u89c6\u7684\u662f\uff0c
x.b=105.005;
printf("\n%f",x.b);
x.c=25;
printf("\n%d",x.a);
\u548c
x.b=1305.005;
printf("\n%f",x.b);
x.c=25;
printf("\n%d",x.a);
\u6267\u884c\u540eprintf("\n%d",x.a);\u7684\u7ed3\u679c\u662f\u4e0d\u4e00\u6837\u7684\u3002\u539f\u56e0\u662f\u6211\u524d\u9762\u5df2\u7ecf\u8bf4\u8fc7\u4e86\u201c\u5728\u5b58\u653e\u6570\u636e\u7684\u65f6\u5019\u662f\u6309union\u91cc\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u5b58\u653e\u7684\u201d\uff08\u4f46\u5b83\u5e76\u4e0d\u8986\u76d6\u539f\u5148\u5df2\u7ecf\u5b58\u653e\u8fc7\u6570\u636e\u7684\u6574\u4e2aunion\u6570\u636e\u533a\uff09\uff0c\u800c\u8bfb\u53d6\u7684\u65f6\u5019\u8fd8\u662f\u6309union\u91cc\u5b9a\u4e49\u7684\u7c7b\u578b\u8fdb\u884c\u8bfb\u53d6\u7684\uff0c\u4f46\u8f93\u51fa\u7684\u8868\u793a\u5f62\u5f0f\u5374\u662f\u4ecd\u7531printf("\n%d",x.c);\u4e2d\u7684\uff08%d\uff09\u63a7\u5236\u7684\u3002
\u4e0a\u9762\u6211\u4eec\u867d\u7136x.c\u90fd\u662f25\uff0c\u4f46\u8bfb\u53d6\u7684\u65f6\u5019\uff0c\u662f\u6309union\u4e2dx.a\u7684\u6570\u636e\u7c7b\u578b\uff08int\u578b\uff09\u8bfb\u51fa\u7684\uff0c\u8fd9\u65f6\u5b9e\u9645\u8bfb\u51fa\u7684\u7c7b\u578b\u5c31\u6bd4x.c\u7684char\u578b\u6570\u636e\u8981\u591a\u4e86\uff0c\u591a\u51fa\u6765\u7684\u6570\u636e\u5c31\u6765\u81f3x.b\u5728\u5185\u5b58\u4e2d\u6b8b\u7559\u7684\u6570\u636e\u3002

1.\u540c\u4e00\u4e2a\u5185\u5b58\u6bb5\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u51e0\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6210\u5458\uff0c\u4f46\u662f\u5728\u6bcf\u4e00\u77ac\u95f4\u53ea\u80fd\u5b58\u653e\u5176\u4e2d\u7684\u4e00\u79cd\uff0c\u800c\u4e0d\u662f\u540c\u65f6\u5b58\u653e\u51e0\u79cd\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6bcf\u4e00\u77ac\u95f4\u53ea\u6709\u4e00\u4e2a\u6210\u5458\u8d77\u4f5c\u7528\uff0c\u5176\u4ed6\u7684\u6210\u5458\u4e0d\u8d77\u4f5c\u7528\uff0c\u5373\u4e0d\u662f\u540c\u65f6\u90fd\u5728\u5b58\u5728\u548c\u8d77\u4f5c\u7528\u3002
2.\u5171\u7528\u4f53\u53d8\u91cf\u4e2d\u8d77\u4f5c\u7528\u7684\u6210\u5458\u662f\u6700\u540e\u4e00\u6b21\u5b58\u653e\u7684\u6210\u5458\uff0c\u5728\u5b58\u5165\u4e00\u4e2a\u65b0\u6210\u5458\u540e\uff0c\u539f\u6709\u6210\u5458\u5c31\u5931\u53bb\u4f5c\u7528\u3002
3.\u5171\u7528\u4f53\u53d8\u91cf\u7684\u5730\u5740\u548c\u5b83\u7684\u5404\u6210\u5458\u7684\u5730\u5740\u90fd\u662f\u540c\u4e00\u5730\u5740\u3002
4.\u4e0d\u80fd\u5bf9\u5171\u7528\u4f53\u53d8\u91cf\u540d\u8d4b\u503c\uff0c\u4e5f\u4e0d\u80fd\u4f01\u56fe\u5f15\u7528\u53d8\u91cf\u540d\u6765\u5f97\u5230\u4e00\u4e2a\u503c\u3002


\u6240\u4ee5\uff0c\u4f60\u5bf9p\u8d4b\u503c\uff0cq\u5c31\u5931\u53bb\u4f5c\u7528\uff0c \u5bf9q\u8d4b\u503c\uff0cp\u5c31\u5931\u53bb\u4f5c\u7528\u3002

\u8054\u5408\u4f53\u4e2d\u5404\u4e2a\u53d8\u91cf\u6ca1\u6709\u5173\u7cfb\u3002\u65e0\u6cd5\u76f8\u4e92\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\u4ed6\u4eec\u662f\u4e92\u65a5\u7684\u3002

union 是共享内存。也就是说,i 和 c[2] 共享内存。
int i; 这里是 short int。
c[0] 在低字节,这是“小端”码,PC/widows 用 小端码 little-endian.
c[1] 在高位。
写成2进制:
x.c[0]=10; 写成2进制:00000000 00001010
x.c[1]=1; 写成2进制: 00000001 00000000
i 写成2进制:00000001 00001010
换算到10进制
i = 1 * 256 + 1*8 + 1* 2 = 266
直接用10进制 i = x.c[1]*256+x.c[0] = 256+10=266.

union联合类型的一般计算形式为:
1.定义union 联合名
{
成员表
};
成员表中含有若干成员,成员的一般形式为: 类型说明符 成员名。成员名的命名应符合标识符的规定。
2.联合体变量的赋值和使用

 对联合体变量的赋值,使用都只能是对变量的成员进行。
联合变量的成员表示为: 联合变量名.成员名
例如,a被说明为perdata类型的变量之后,可使用 a.class,a.office
不允许只用联合变量名作赋值或其它操作。 也不允许对联合变量作初始化赋值,赋值只能在程序中进行。一个联合体变量, 每次只能赋予一个成员值。一个联合变量的值就是联合变员的某一个成员值。

哪位兄弟解释的很好了,你要知道union中的变量 是共享内存,而且共享那个最大的存储内存,因为此时union中有int和char有两中类型,当然系统分配就是两个字节,算法就跟他是一样的

一看就知道是二级C题,很没有实用性

0x010a = 266

扩展阅读:结构体中的union联合体 ... 联合体登记是什么 ... c++编程 ... c语言union用法 ... 联合体union和struct ... c语言联合体和结构体 ... 联合体牵头人是总包吗 ... 什么叫联合体招标 ... c# 全局变量 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网