223k views
5 votes
How to use union in the real world?​

User Iamdeit
by
5.7k points

1 Answer

4 votes

Answer:

To overcome such type of real world problems we use union. Unlike structures union occupies single memory location to store all its members. So, unions are helpful when you want to store value in single member from a set of members. Size of union is defined according to size of largest member data type.

Explanation:

In union, all members share the same memory location. For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y. #include <stdio.h> // Declaration of union is same as structures.

User Joshua Kravitz
by
5.3k points