Click here to Skip to main content
12,693,693 members (35,587 online)
Rate this:
 
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:

-----------------------<br />|         512         |<br />-----------------------<br />| 00000010 | 00000000 |<br />-----------------------<br />|    2     |     0    |<br />-----------------------


  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.
:)

  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<br />  {<br />       BYTE    Mask;<br />       struct {<br />            BYTE    On1                 : 1;<br />            BYTE    On2                 : 1;<br />            BYTE    On3                 : 1;<br />            BYTE    On4                 : 1;<br />            BYTE    OnTOF               : 1;<br />            BYTE    Unused              : 1;<br />            BYTE    MasterOn            : 1;<br />            BYTE    ScanOn              : 1;<br />        } Bits;<br /> } 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;<br />c.Mask = 1 << 3 | 1 << 7;<br />SendChannels (c);

or I say:
__ChannelsOn c;<br />c.Mask = 0;<br />c.Bits.On3 = 1;<br />c.Bits.ScanOn = 1;<br />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<br />{<br />   int nType;<br />   union {<br />      int nInt;<br />      long lLong;<br />      DWORD dwDword;<br />      BSTR bstr;<br />   } Var;<br />};


I hope that helps a bit,

Iain.

  Permalink  
  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 3 Apr 2009
Copyright © CodeProject, 1999-2017
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