Difference Between Structure and Union

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.

/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

The core idea of develop AI is bulding machines and alogrithms to

2 / 10

Which of the following semiconductor is mostly used to construct electronic circuits?

3 / 10

'.BAK' extension usually refers to what kind of file?

4 / 10

Phones that offer advanced features not typically found in cellular phones, and are called

5 / 10

The output printed by a computer through a printer on the paper is called

6 / 10

The conductivity of semiconductor materials

7 / 10

Which number system has a base 16

8 / 10

Geo-stationary satellite revolves at –

9 / 10

Which of the following AI domain attempts to extract information from spoken and written words using algorithms?

10 / 10

Android is -

Your score is

0%

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 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.

Structure vs Union 1

Comparison Table

Parameters of ComparisonStructureUnion
DefinitionStructure allocates different items to different memory locations.Union \allocates different memory items to the same location.
Syntaxstruct struct1{Data type 1;Data type 2;…}variable 1, variable 2,…;union union1{Data type 1;Data type 2;…}variable1; variable2, …;
Keywordstruct union 
Memory All data types are in separate memory locations.All data types are in the same memory location.
SizeStorage 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.

structure

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{

Data object 1;

Data object 2;

…;

};

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. 

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.

References

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

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️