Learn Programming through LIVE online classrooms

C and Data Structures and Algorithms

Difference between Structure and Union in C Language

Posted in C, Data Structures and Algorithms

In C/C++, Structures and Union are two user-defined data types. In this blog post, we will try to understand how they both work and how exactly are they different from each other.

Understanding a Structure (struct)

Struct is a user-defined data-type that’s used to store a combination of data which can potentially belong to different data-types. To explain further, think about how you can store information about a student in C/C++, where, you need to store the following parameters:

  • Name
  • Class
  • Phone Number
  • Email Address

One way to go about it is to store it in 4 different arrays:  name[], class[], phone[], email[]. Basically, name[i] will represent the name of the ith student. class[i] will represent the class of the corresponding student. phone[i] will represent the phone number and email[i] will represent the email address of ith student.

The advantage of this method is that it is the easiest one to think of. The downside is that it is quite difficult to manage a student this way. Here, we have just four parameters. Imagine a situation where we have 400 parameters associated with a student. One simply cannot manage 400 arrays. This is where structs come into the picture.

Defining a Structure

Through struct, we can define the structure of a single student as follows:

struct Student {
 string name;
 int class;
 string phone;
 string email;
}

We can then simply define an array of students as: Student students[10].

The above structure beautifully captures the details of a student. Here are some of the advantages of this implementation:

  • All the variables of a particular student are packaged together and it is much cleaner.
  • The parameters of a particular student are all stored sequentially in the computer memory. This leads to far more efficient caching of student data.

Understanding a Union

Suppose we are conducting a survey of various people living in our society. As a part of this survey, our aim is to try to record the height (in mm) and weight (in kgs) of various people. The height could be something like 1700mm. Weight could be 74.23 kgs. Height is always an integral value (in mm) while weight could be fractional. The issue here is that for some people we have only height data. For others, we have only weight data. The obvious way to store such data is to create a struct.

struct Person {
 int height;
 double weight;
}

The issue here is that we only have either the height or the weight available for each person. However, we are allocating space for both. This leads to a wastage of memory. What if we could store only height for those whose height is available and only weight for people where we have the weight value at our disposal? It would make things much simpler and also save memory. This is where unions help.

union Person {
 int height;
 double weight;
}

Now, we can easily store the details of a Person as follows:

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

When we create the person1 object, space is allocated only for height. No space is allocated for weight. Similarly, when we create the person2 object, space is allocated only for weight. No space is allocated for height. Here, depending on architecture, we are saving 4 - 8 bytes on each of the first as well as the second object. Imagine if we had data for a billion people, how much bytes we would be able to save?

Let us take a look at code

The output for this code is as follows:

Observe here that in the first case, the size of person1_union and person2_union is each 8 bytes. This is because a Union needs to store the size of the largest data type as a part of it. For instance, if a union stores a character and a double value, its size will be that of the double value because even if it is storing a character, there should be space allocation for double value.

The great thing here is the amount of size savings that we have achieved - literally half the size as that of struct. Imagine if we are storing such data for each person on earth - we will save several gigabytes of storage space/cost.

Difference between Structure and Union

Let us summarize our understanding of Union and Struct in the form of a table to highlight the differences between structure and union:

Struct Union
Used for storing various data types that when aggregated, represent a user-defined data type. Used for storing one of many data types that are available.
Occupies space for each of the inner parameters. Occupies space equivalent to the parameter with the highest size.
All members store some value at any point in time. Exactly one member stores a value at any particular instance.

 

Harshita Srivastava

Harshita Srivastava

Harshita is a graduate from Indian Institute of Technology, Kanpur. She is a technical writer and a blogger. An entrepreneurship and machine learning enthusiast, who loves reading and is a huge fan of Air Crash Investigation! View all posts by the Author

Leave a comment

Your email will not be published
Cancel
Pamela Simpson
Pamela Simpson

How are structs in C different from classes in C++?

Leonel Sledge
Leonel Sledge

The main difference between structure and class is that structure members have public access by default and class members have private access by default.

Raquel Bryant
Raquel Bryant

Which one is the best to use in C: structure or union?

Asa Devore
Asa Devore

Both of have their own characteristics and to choose one is depends on the need of your program. Union is a type which will store different data types in same memory space to save the memory while Structure is a union in which each data type have its own memory. If you want to save memory, you can go with Union.

Theresa Gregory
Theresa Gregory

Why C is structured programming?

Federico Mott
Federico Mott

C divides the large problem into smaller modules which is called functions or procedures. The program which solve the problem is the collection of such functions. That’s why C is called a structured programming language.

Lois Zimmerman
Lois Zimmerman

Can structs have methods?

Filiberto Bader
Filiberto Bader

Structure can have methods, indexes, fields, properties, operator methods and events. Structure can have defined constructor but not destructors.

Krystal Cobb
Krystal Cobb

What is a structure in OOP?

Lon Lancaster
Lon Lancaster

OOPs is a structured language in which structure goes to a different level than procedural language. As in OOP you isolate your code into classes.

Abraham Foster
Abraham Foster

What is the difference between structure and class?

Edison Fink
Edison Fink

• Structure is a value type that is why its object is created on Stack memory. While, Class is a reference type and its object is created on Heap memory.
• Structure does not support the inheritance and cannot inherit another class. While Class can inherit the another class.
• Structure can only have the parametrized constructor. Class can have constructor and destructor of all types.
• Structure object can be created without using the new keyword. Class object cannot be created without using the new keyword.

Caroline Crawford
Caroline Crawford

Where is Union used in C?

Lucien Dunning
Lucien Dunning

Union is used to feed all information within the same memory block to save memory by using a general purpose buffer to store temporary data from different points of your program flow. Also when you need to interpret the same memory in different ways.

Mona Nelson
Mona Nelson

Can we define structure inside Union?

Junior Lumpkin
Junior Lumpkin

Yes, this is possible to define structure within union. A brief example of it:
void main() {
struct student {
char name[30];
char sex;
int rollno;
float percentage;
};
union details {
struct student st;
};
union details set;
clrscr();