6. Unions
|
|
A union is a variable that may hold objects of different types/sizes in the same memory location. |
|
Example: |
union data {
int idata;
float fdata;
char∗ sdata ;
} d1, d2, d3;
d1.idata=10;
d1.fdata=3.14F;
d1.sdata="hello world”; |
|
|
The size of the union variable is equal to the size of its largest element. |
Important: The compiler does not test if the data is being read in the correct format. |
union data d; d.idata = 10;
float f = d.fdata; /∗ will give junk ∗/ |
|
|
|
|