. , , , . (, ) union. , , , ( ). .
UNI :
union {
long L;
unsigned i1;
int i2;
char c[4];
} UNI;
UNI :
long L
unsigned i1
int i2
char c[4]
.. . :
cout << &UNI .L; //
cout << &UNI. i1;
cout << &UNI. i2;
cout << &UNI .c;
, , , . , . , . .
. - , const. - .
, , :
_. _
(*__). _
__ -> _
__. _
!!! - ( ). :
enum week {sun, mon, tues, weds, thurs, fri, sat};
union { //
int i;
week w;
};
i = 6; if ((w == sun) || (w == sat)) cout << !;
, .
, , : .
, , :
union {
int ival;
float fval;
char cval[4];
} uval;
uval ; .
:
|
|
.
, . :
union int_flt {
int i;
float x;
} n = {0}; // () i .
n. i = 7; //
cout << n. i << endl; //
n. x = 7. 0; //
cout << n. x << endl; //
.
:
union
{float f;
unsigned long k;
} FK;
FK. f = -256.5, :
cout << hex << FK.k; // 3804000 16.
:
union
{double f;
char h[8];
} FLH;
FLH FLH .f, FLH .h [0], FLH .h [1], ..