Difference Between Structure and Union

Alice williams
3 min readApr 25, 2021

--

In this blog post, we’ll try to understand how they work and how exactly are they distinct from one another.

What is Structure?

Struct is a user-defined data-type that is used to keep a combination of data which can potentially belong to different data-types. To describe further, Consider how you can store information about a pupil in C/C++, where, You Have to store the following parameters:

Name

Class

Phone

Number

Email Address

One way to go about it would be to store it in 4 different arrays: name[], class[], phone[], email[]. Fundamentally, name[I] will represent the name of the ith student. Class[I] will signify the class of the corresponding student. Telephone [I] will represent the contact number and email[I] will signify the email address of ith student.

The advantage of this process is that it is the simplest one to consider. The downside is that it is fairly difficult to manage a student such a manner. Here we have just four parameters. Imagine a situation where we have 400 parameters associated with a pupil. One simply cannot manage 400 arrays. This is really where structs come in the picture. Defining a Structure During struct, we can define the structure of a single student as follows:

struct Student
String name;
int category;
chain phone;
string email;

We can then easily define a range of pupils as: Student students[10].The above structure beautifully captures the specifics of a pupil. Each of the variables of a specific pupil are packaged together and it is much cleaner.

The parameters of a specific student are all stored sequentially in the computer memory. This results in much more efficient caching of pupil information.

What is Union?

Suppose we are running a survey of different individuals living in our society. As part of the survey, our aim is to attempt and record the height (in mm) and weight (in kgs) of various people. The elevation could be something like 1700mm. Weight could be 74.23 kgs.

Height is always an integral value (in mm) while weight might be fractional. The issue here is that for some people we’ve got only height data. For others, we have only weight data. The obvious way to store such data is to make a struct.

struct Person
int height;
double weight;

The issue here is that we only have the height or the weight available for each person. However, we are allocating space for the two. This leads to a wastage of memory. What if we can keep only height for people whose height would be the accessible and only weight for individuals where we possess the weight value at our disposal? It might make things much easier and also save memory. This is where unions assist.

union Person Now, we can easily save the specifics of an Individual as follows:

union Person person1 = ;
union Person person2 = {.weight=74.23};

No space is allocated for height. Here, based on the architecture, we’re conserving 4–8 bytes on each of the first as well as the next object. Imagine if we had data for a million individuals, how much bytes we would Have the Ability to save.

Conclusion

That is about the primary difference between Structure and Union. Both of these are the form of information structure. Both of these would be the aggregated info types which have a variable of distinct data types.

--

--

Alice williams

My Self Alice, I am Coding Lover Programming Likes as python, C, C++ and Selenium Automation testing etc.