1
12/31/2022
Unions
union
int x;
double y;
};
union number a;
2
12/31/2022
union
int x; double y;
} a;
a.x = 5;
printf(“%d”, a.y); /* you will see some garbage on the screen, need not be 5.0 */
3
12/31/2022
You should remember which member has the value !
typedef enum flag Flag;
union number {
int x; double y;
};
struct Numb{
Flag f;
union number a;
};
struct Numb n;
n.a.x = 5;
n.f = first;
4
12/31/2022
Intialization
int x; double y;
};
union number a = {10}; /* OK */
union number b = {10, 1.43}; /* error */
5
12/31/2022
Operations with unions
6
12/31/2022