Difference Between Structure and Union

Main difference

Structure and union are user-demarcated data types that contain variables of different data types. They both have the same syntax for defining, declaring variables, and retrieving members. Still, there are many differences between structure and union. In the structure, each member gets a separate space in memory. In union, the total allocated memory space is equal to the largest member. All other members share the same memory space. This is the biggest difference between structure and union.

What is the structure?

The structure uses all the memory of its member. In structure we can admit any member in any sequence.

What is Union?

Union uses the memory space of the largest member. In union we can access only that variable whose value has been stored recently.

Key differences

  1. In the structure, each member gets a separate space in memory. In union, the total allocated memory space is equal to the largest member. All other members share the same memory space. This is the biggest difference between structure and union.
  2. In structure we can admit any member in any sequence. Whereas in union we can access only that variable whose value has been stored recently.
  3. All members can be prepared by declaring the structure variable. Only the first member can be initialized when declaring the bind variable. In the above example we can only modify the roll variable not at the time of variable declaration.
  4. Within a structure, all members get memory allocated, and members have addresses that increase as decelerators are read from left to right. That is, all members of a structure start at different offsets from the base of the structure. For a union compiler allocates memory for the largest of all members and in a union all members have zero offset from the base, the container is large enough to hold the WIDEST member and the alignment is appropriate for all the types in the union.
  5. Within a structure, all members are allocated memory; therefore, any member can recover at any time. When retrieving data from a union, the type being retrieved must be the most recently stored type.
  6. One or more members of a structure can be initialized at a time. A union can only be initialized with a value of the type of its first member; therefore, the union u described above (during the example declaration) can only be initialized to an integer value.
  7. The prerequisite amount of memory to store a structure variable is the sum of the size of all members. On the other hand, in the case of unions, the amount of memory required is always equal to that required by its largest member.
  8. With a join, it’s only hypothetical to use one of the items, because they’re all stored in the same place. This makes it useful when you want to store something that could be of multiple types. A structure, on the other hand, has a separate memory location for each of its elements and they can all be used at once.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Back to top button