Union
-
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?
---------------------------- 286? WOWW!:-O
The union looks something like this:
-----------------------
| 512 || 00000010 | 00000000 |
| 2 | 0 |
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
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?
---------------------------- 286? WOWW!:-O
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 codes0
and2
. Incidentally 0 corrensponds to string terminator soch
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 get0 * 2^0 + 2 * 2 ^ 8 = 512
. This explains the output of the_8086 wrote:
cout<<x.i<<endl;
line. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The union looks something like this:
-----------------------
| 512 || 00000010 | 00000000 |
| 2 | 0 |
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
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 codes0
and2
. Incidentally 0 corrensponds to string terminator soch
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 get0 * 2^0 + 2 * 2 ^ 8 = 512
. This explains the output of the_8086 wrote:
cout<<x.i<<endl;
line. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Such a small thing has got this much hidden! That's all about that crazy union :). Thanks mate.
---------------------------- 286? WOWW!:-O
-
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?
---------------------------- 286? WOWW!:-O
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.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!