- C Unions are essentially the same as C Structures
- except that instead of containing multiple variables each with their own memory a Union allows for multiple names to the same variable
- that means it allocatees the memory equal to the size of the variable of the maximum size.
- These names can treat the memory as different types.
Combining this with a structure allows you to create a “tagged” union which can be used to store multiple different types, one at a time.
For example, you might have a “number” struct, but you don’t want to use something like this:
struct operator {
int intNum;
float floatNum;
int type;
double doubleNum;
};Because your program has a lot of them and it takes a bit too much memory for all of the variables
struct operator {
int type;
union {
int intNum;
float floatNum;
double doubleNum;
} types;
};