Difference Between Structure and Union

Similarities between Structure and Union

 

  1. Both are user-defined data types used to store data of different types as a single unit.
  2. Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
  3. Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
  4. A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
  5. ‘.’ operator is used for accessing members.

Differences

A structure is defined by the struct statement, whereas a union is defined by the union statement. Both store data, but while the union allows storing different data types in the same memory location, a structure is primarily used to represent a record.

Memory Allocation In StructureIn C programming language, both structure and union are two different types of user defined data types, which means that they are two different ways that can be used to store data. Both structures and unions support only assignment = and sizeof operators; however, both are slightly different which makes them better suited for different tasks.

A structure is defined by the struct statement, whereas a union is defined by the unionstatement. Both store data, but while the union allows storing different data types in the same memory location, a structure is primarily used to represent a record.

Additionally, while a structure allows the combining of different data types, a union can be defined by many members, but each member can only contain a single value at any given time. The amount of memory required to store them also differs. A structure requires an amount of memory which is equivalent to the sum of the size of all the members. The amount of memory required to store a union, on the other hand, is always equal to that required by its largest member.

In a structure, each member has their own memory space, while in a union, a single block is used by all the members of the union. Furthermore, while altering one value in a structure will not affect the other members, the same cannot be said of a union. In a union, altering any one value of any member will affect the other member values.

Memory Allocation in UnionIn a structure, an individual member can be accessed at any time and all of these members can be initialize at once. Whereas in a union, only one member can be accessed at any given time and only the first member of a union can be initialized at a time.

It should be noted that structures and unions cannot appear as operands of the equality ==, inequality !=, or typecast operators; but both structure and union can be passed by value to functions and returned by value by functions