Assigning two values with an union variable

2019-02-28 03:48发布

问题:

The variable a is assigned by value 10 and the variable b is assigned by 20 with the union variable v. Then it gives the output of a is 20 instead of 10. I don't get it.

#include<stdio.h>  
int main()  
{
   union var
   {
       int a, b;
   };
   union var v;
   v.a=10;
   v.b=20;
   printf("%d\n", v.a);
   return 0;
}  

I executed the program and I got 20 as output.

回答1:

union overlays all the listed members on top of each other (although some may extend farther than the overlapping initial sections), so assigning to either .a or .b is writing over the same part of memory. With that in mind, the output should make sense.

You probably were thinking of struct's behavior if you were expecting an output of 10.

In really twisted scenarios, it's possible to have parts of different values stored in a union simultaneously, but generally the partially overwritten values will be assumed corrupt. For example, this:

union {
    char a;
    struct { char ba; char bb; } b;
} s;

can store s.a and s.b.bb at the same time, but since s.a overlaps s.b.ba, assigning to s.a stomps on s.b.ba, and by implication all of s.b is no longer trustworthy.

Often unions intended to store different types will be embedded in a struct whose first member records which union member is in use:

struct {
    int type;
    union {
       char ch;
       int n;
    } datum;
} atom;

Here, type probably would contain an enumerated value to indicate whether datum.ch or datum.n was in use in the atom.



回答2:

This is an instructive question. Try reversing the assignments, as:

   v.b=20;
   v.a=10;

And see the result. If you want a and b to co-exist rather than overlay each other then use a structure, as:

#include<stdio.h>  
int main()  
{
   struct var
   {
       int a, b;
   };
   struct var v;
   v.a=10;
   v.b=20;
   printf("%d\n", v.a);
   return 0;
}  

and you should get the result want. However, the var structure now contains two integers.



回答3:

In union at a time you can assign one value. In union the values are overlapped with each other. So you are doing the wrong with assigning values to two elements of union. It will always show the last assigned value. So if you are accessing the value of a it will show the last updated value.



回答4:

A union differs from a struct in these two ways:

  • Space is allocated for the largest type present (since they're both int, the size of the union overall is int)
  • Only one value may be stored in a union at any time.

The last value you store into v is 20, so one should expect that value back when printing any member of the union.

If you wanted to have access to both members, then you would want to use a struct instead.



回答5:

You should read about union:

  • http://en.wikipedia.org/wiki/Union_type
  • http://www.go4expert.com/articles/unions-c-powerful-feature-c-t15/
  • Why do we need C Unions?

This will help you understand what happens - basically all members of a union are stored at the same address in memory.



回答6:

Lets assume, a primitive int size is 32 bits.

The concept of union data type in C/C++ is: it enables us to store different data types in the same memory location. We can define a union with many members, but only one member can contain a value at any given time. The size of a union is sufficient to contain the largest of it's members.

In your case: both 'a' & 'b' is int type, so the size of the 'var' union is 32 bits.

Now imagine binary array of 32 in size. Lets say it A[32]. The least 5 significant bits store 01010, i.e. binary form of 10 which is assigned to 'v.a'. Next when you assigned 20 to 'v.b', then 10100 (binary form of 20) will replace 01010. So currently the union v having 10100 in it's memory space.

When you are accessing the 'v.a', it is returning the current value of that memory location.

See the link wikipedia link



标签: c unions