Difference Between Structure and Union (With Table)

Structure and Union are used in the world of programming to facilitate the function of variables, the flow of data, and the ability to manipulate data. The handling of variables is a very important process, as that helps us define complex algorithmic structures that can process data. In the same way, the programming language C offers two variable types, called structure and union.

On the face of it, they both seem to possess pretty similar functions, and in some cases, they can be used for the same function too! However, there are several differences that these possess, knowing which helps differentiate these terms.

Structure vs Union

The main difference between structure and union is that separate memory locations are allotted to each member of a structure, however, a union contains a single memory location for all its data contents.

Comparison Table Between Structure and Union

Parameters of Comparison

Structure

Union

Definition

Structure allocates different items to different memory locations.

Union \\allocates different memory items to the same location.

Syntax

struct struct1{Data type 1;Data type 2;…}variable 1, variable 2,…;

union union1{Data type 1;Data type 2;…}variable1; variable2, …;

Keyword

struct 

union 

Memory 

All data types are in separate memory locations.

All data types are in the same memory location.

Size

Storage size is the sum of the size of all data items.

Storage size is the value of the largest data type.

What is Structure?

Structures are the variables that can hold many types of data items at the same time. It is, however, worthwhile to note that the data items held by a structure can be of different data types. Thus, structure is a very useful way for the storage, accessing, and manipulation of data. A structure is defined using the ‘struct’ statement.

To understand what a keyword is, we will have to look a bit into the concept of the language. In any programming language, not just C, we need to use a keyword or a command that helps the compiler recognize a certain function that has been called, and it will execute accordingly. 

The syntax of a structure follows the following pattern:

struct structure_name{

char firstName[10];

char lastName[10];

Int age;

char address[20];

};

This structure has been named ‘structure_name’, and can be called anywhere in the function after it has been defined. It can be called a number of times also, as per requirement. We can see that the structure has a few data items as its attributes.

They are the first name of a person, the last name of that person, their age, and their address. Notice how there are more than one data type present in the structure. All these data types will have separate memory locations where they will be stored. 

By default, all the members in a structure are public. By public, we mean that all other functions and objects will be able to access all the data types that are present in the structure. This property can however be changed to private, so as to protect the data.

What is Union?

A union is a data type, that can store various data items inside it. This is found in the programming language C, where this helps in the insertion, manipulation, and access of data in a program. It is, however, worthwhile to note that unions can not hold data items of different data types, and so this limits its capabilities as compared to structures to some extent.

When we define or call a union, it is pretty much the same method the way we call a structure. The keyword for union is ‘union’ only, and this is followed by the name of the union the programmer wishes to give. The syntax of union is as follows:

union union_name{

Similar to structures, the union, after being defined, can be used anywhere in the function and also in other classes, and objects. The union can be used as many times as required. The memory space occupied by the union will be the same as the memory required to accommodate the largest data type mentioned in the union. 

Main Differences Between Structure and Union

  1. The main difference between Structure and Union is that structures are variables that can accommodate other variables and allocate them separate memory spaces, while unions allocate the variables the same memory space.
  2. The keyword of structure is ‘struct’, while the keyword of union is ‘union’.
  3. Structures can store multiple values at a single time, while unions can store only a single value at a single time.
  4. Structures can help to view a single memory location in only one way. Unions help to view a single memory location in many ways.
  5. A structure can not be anonymous, however, a union can be declared as anonymous.

Conclusion

The various functions, objects, and features of a programming language define whether it will be useful to the programmer or not. The languages have different features, which define what purpose it will serve. Some languages are better built for developing web pages, some are built to develop games, some to develop high-performance software, and some for android applications.

There are numerous purposes for numerous languages. The fact that some languages are object-oriented, while some are program-oriented. The object-oriented languages help us add and remove modules. This facilitates to make software, which helps us make our lives easier.

References

  1. https://www.bell-labs.com/usr/dmr/www/chist.pdf
  2. https://asa.scitation.org/doi/pdf/10.1121/1.401205