Newbie question on unions

From: patleonard (cppman_at_ptd.net)
Date: 10/11/04


Date: Sun, 10 Oct 2004 21:04:50 -0400

The following code produces this output:

Characters: XY
Integer: 22872

Where does the integer 22872 come from?

#include <iostream.h>

int main()
{
 union test_union{
  short int i;
  char ch[2];
 } unvar;

 unvar.ch[0] = 'X';
 unvar.ch[1] = 'Y';

 cout << "Characters: " << unvar.ch[0] << unvar.ch[1] << '\n';
 cout << "Integer: " << unvar.i << '\n';

 return 0;
}

It may be a stupid question, but I don't know the answer. Any response will
be helpful, thank you.



Relevant Pages

  • Re: Checking for a keypress on Linux ?????
    ... the short delay in kbhit() ... > int getch; ... > * returns the number of characters available to read. ... > static struct termios Otty, ...
    (comp.os.linux.development.apps)
  • Re: [PATCH] Fix console utf8 composing
    ... So, the result is that the result of the composing operation is still taken from the accent_table, and thus cannot be more than "unsigned char" allows. ... in UTF-8 mode, it is possible to generate characters from Latin-1 set by composing, and they are generated correctly. ... +static int use_unicode; ...
    (Linux-Kernel)
  • Re: [PATCH] console UTF-8 fixes
    ... as substitute glyph ... don't ignore zero-width characters (except for a few zero-width spaces ... print an extra space for double-wide characters for the cursor to stand ... int first; ...
    (Linux-Kernel)
  • Re: Get ASCII value for character when higher than 127
    ... The reason I want to store the int-values for the characters in stead ... I also imagined, that if I have the int value, I can perform some ... char timeString; ... strcat; ...
    (microsoft.public.vc.language)
  • Re: Get ASCII value for character when higher than 127
    ... The reason I want to store the int-values for the characters in stead ... I also imagined, that if I have the int value, I can perform some ... char timeString; ... strcat; ...
    (microsoft.public.vc.language)