Union is another user defined data type.
The difference between union and struct is that; struct uses a contagious memory allocation for each individual item inside it; whereas union uses the size of biggest element for allocation of memory.
union segment1 {
int x1;
char y2;
};
// sizeof segement1 union is 4 bytes
struct segment2{
int x1;
char y2;
};
// sizeof segement1 union is 8 bytes
Leave a Reply