Unions basically represent two or more different views of the same data.
A good example would be a data record in a file (organized with a fixed record length). One way of looking at the record, say, would be as a sequence of 16 bytes. Another way would be to look at it as structured data. Something like this:
union
{
unsigned char raw_data[16];
struct
{
char name[12];
short age;
short height;
}
}
Code that reads or writes data from/to disk would be using the raw_data member. Code that actually interprets the data (e.g., presents it in a UI) would be using the struct representation.
For more real-life examples, check out this StackOverflow discussion:
It is just a design choice whether to put union inside the structure or a structure inside a union. Similar to the example given by Toth there is a union in Linux data structures also. Which is IP address. It is:
union ipv4addr {
unsigned addr;
char octets[4];
};
Generally we want to refer to the 32-bit value directly, at that time we can use the addr part of the API or if we want to access individual bytes of the IP address then we can use octets. Thus it is the way of giving different meaning to a single API.