Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI C MFC ATL WTL STL
union u
{
char ch[2];
int i;
};

int main()
{

union u x={0,2};
cout<<x.ch<<"\n\n\n";
cout<<x.i<<endl;
return 0;


}

Why does this print 512???[confused] What is this x={0,2}; exactly doing?

Posted 3-Apr-09 8:39am
_8086424
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The union looks something like this:

-----------------------
|         512         |
-----------------------
| 00000010 | 00000000 |
-----------------------
|    2     |     0    |
-----------------------


  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Is doing politely what you asked with your code.

The character array and the integer share the same memory space (at least the first two bytes), hence assigning one of the two will affect the other (you know that: it is a union, after all... [rolleyes]).

_8086 wrote:
union u x={0,2};

Here the compiler initialise the ch member (this surpised a bit me) of the union with the characters having ASCII codes 0 and 2. Incidentally 0 corrensponds to string terminator so ch eventually contains an empty string, this explains the output of the
_8086 wrote:
cout<<x.ch<<"\n\n\n";

line.

Such a initialization affect also the integer (i) member, and since you computer is a little endian one, you get 0 * 2^0 + 2 * 2 ^ 8 = 512.
This explains the output of the
_8086 wrote:
cout<<x.i<<endl;

line.
Smile | :)

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Unions are powerful things, but until you realise that the parts share the same memory, you'll struggle. David's picture and Carlo's talk both help, I hope.

They are very powerful in their limited way. Here's a sample of my code (no real secrets here):
	union __ChannelsOn
	{
		BYTE	Mask;
		struct {
			BYTE	On1					: 1;
			BYTE	On2					: 1;
			BYTE	On3					: 1;
			BYTE	On4					: 1;
			BYTE	OnTOF				: 1;
			BYTE	Unused				: 1;
			BYTE	MasterOn			: 1;
			BYTE	ScanOn				: 1;
		} Bits;
	} ChannelsOn;


I have some hardware that has a command I send to it to turn channels on and off. I send a byte made up of flag bits. I could say:
__ChannelsOn c;
c.Mask = 1 << 3 | 1 << 7;
SendChannels (c);

or I say:
__ChannelsOn c;
c.Mask = 0;
c.Bits.On3 = 1;
c.Bits.ScanOn = 1;
SendChannels (c);


Both do the same thing - but which is more readable?

They are also used to make the variant structure, used to talk with COM/VB.
It's equivalent to:
struct VARIANT
{
   int nType;
   union {
      int nInt;
      long lLong;
      DWORD dwDword;
      BSTR bstr;
   } Var;
};


I hope that helps a bit,

Iain.

  Permalink  
  Print Answers RSS
0 Sergey Alexandrovich Kryukov 205
1 Zoltán Zörgő 200
2 Peter Leow 160
3 BillWoodruff 85
4 bling 70
0 Sergey Alexandrovich Kryukov 9,543
1 OriginalGriff 6,901
2 Peter Leow 4,737
3 Zoltán Zörgő 4,404
4 CHill60 2,932


Advertise | Privacy | Mobile
Web02 | 2.8.150129.1 | Last Updated 3 Apr 2009
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100