工会整为什么垃圾的价值? [英] Why garbage value in union integer?

查看:125
本文介绍了工会整为什么垃圾的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关以下程序INT印有垃圾的价值,但预期是初始值50,是否有在输出该值的具体原因是什么?

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;联合数据
{
    int类型的;
    浮℃;
    炭B〔10〕;
};诠释的main()
{
    联合数据X;
    x.a = 50;
    的strcpy(x.b,你好);
    x.c = 21.50;
    的printf(x.a:=%d个\\ N,x.a);
    的printf(x.b:=%S \\ n,x.b);
    的printf(x.c:=%F \\ N,x.c);
    返回0;
}

输出:

x.a:= 1101791232
x.b:=
x.c:= 21.500000

更新:对于下面的一切,提到做工精细类似的计划,我没有线索怎么这会产生预期的结果

的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;联合数据
{
   INT I;
   浮F;
   焦炭海峡[20];
};诠释的main()
{
   联合数据的数据;   data.i = 10;
   的printf(data.i数:%d \\ n,data.i);   data.f = 220.5;
   的printf(data.f:%F \\ N,data.f);   的strcpy(data.str,C程序设计);
   的printf(data.str:%S \\ n,data.str);   返回0;
}

输出:

data.i:10
data.f:220.500000
data.str:C编程


解决方案

一个联盟将​​使用相同​​的存储位置的所有成员。您可以设置 XA 但是当您设置 XC 它会覆盖的XA 以及

在二进制,整型值 1101791232 是一样的浮点值 22.50 ,您可以轻松地验证即:

 浮动F = 21.50;
  为int * IP =(INT *)及F;
  COUT<< *的iP;

工会数据看起来在内存中像这样(每个X是一个字节):

  XXXXXXXXXX<  - 联合数据
XXXX< - int类型的
XXXX< - 浮动Ç
XXXXXXXXXX< - 焦B〔10]

所以,写入或c将覆盖等,再加上字符数组的第4个元素。

至于你的第二个例子,尝试设置三个成员以及事后才输出自己的价值观,他们将再次成为垃圾。它只能是因为你的输出后,立即设置和覆盖了。

For the below program int is printed with garbage value but expected is the initialized value 50, Is there any specific reason for this value at the output?

#include <stdio.h>
#include <string.h>

union data
{
    int a;
    float c;
    char b[10];
};

int main()
{
    union data x;
    x.a = 50;
    strcpy(x.b,"hello");
    x.c = 21.50;
    printf ("x.a:= %d\n",x.a );
    printf ("x.b:= %s\n",x.b );
    printf ("x.c:= %f\n",x.c );
    return 0;
}

Output:

x.a:= 1101791232
x.b:= 
x.c:= 21.500000

Update: For similar program as mentioned below everything work fine, I have no clues how this could produce the expected results

#include <stdio.h>
#include <string.h>

union 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, "C Programming");
   printf( "data.str : %s\n", data.str);

   return 0;
}

Output :

data.i : 10
data.f : 220.500000
data.str : C Programming

解决方案

A union will use the same storage location for all its members. You set x.a but then when you set x.c it overwrites the memory location of x.a as well.

In binary, the integer value 1101791232 is the same as the float value 22.50, and you can easily verify that:

  float f = 21.50;
  int * iP = (int*)&f;
  cout << *iP;

The union data looks something like this in memory (each X is a byte):

XXXXXXXXXX <- union data
XXXX       <- int a
XXXX       <- float c
XXXXXXXXXX <- char b[10]

So writing to a or c will overwrite the other, plus the first 4 elements of the char array.

As for your second example, try setting the three members and only afterwards output their values, and they will be garbage again. It only works because you output immediately after setting and before overwriting.

这篇关于工会整为什么垃圾的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆