Difference between Structure and Union

Share:

In this chapter, we will explore the concepts of structure (struct) and union in C language. Both structure and union are user-defined data types in C that allow you to combine data of different types together. While at first glance they may seem similar, there exist key differences on how they allocate memory and handle data storage. We'll be discussing these differences in detail and providing illustrative code snippets for each.

Structures in C are used to group different types of related variables under a single name. Suppose you want to store information related to an employee which include name, ID, and salary. It would be ideal to group these variables under a single name using 'struct' for easier management. The ‘struct’ keyword is used to define a structure. Here is an example:

struct Employee {
  int id;
  char name[50];
  float salary;
}

In this example, a structure named 'Employee' is defined with three variables named 'id', 'name', and 'salary'. Each variable in the structure is called a 'member'. So, there are three members: id, name, and salary in the 'Employee' structure.

Now, let's define Unions in C. A union in C programming is also a user-defined data type which can hold members of different types. Union uses a single memory location to hold more than one variables. However, only one of its members can contain a value at any given time. Here is a basic example of a Union:

union Job {
  //defining a union
  int id; 
  char name[50];
  float salary;
}

In the above example, ‘Job’ is a union type that has 3 members i.e., 'id', 'name', and 'salary'. Unlike struct, all members of a union share the same memory location. Because of this, only one member can hold a value at any given time.

The main differences between Structure and Union in C are related to memory allocation and usage:

  1. Memory allocation: In a structure, each member has its own memory location, whereas all members of a union share the same memory location. The total memory size of a struct is the sum of the memory size of all members. For a union, the memory size is that of the largest member.

  2. Usage of values: In a structure, all members can store values at the same time. In a union, only one member can store a value at any given time.

To illustrate, imagine if Employee is a structure and Job is a union, both having an int (4 bytes), a char array of 50 characters (50 bytes) and a float (4 bytes). Here, Employee will occupy a total of 58 bytes of memory, while Job will only occupy 50 bytes of memory, equivalent to the size of its largest member.

In conclusion, the choice between using a structure or a union mainly depends on how you intend to use them. If you need to store multiple related variables and access them all at once, a structure would be ideal. However, if you only need to access one variable at a time, from a pool of related variables, then a union would be more suitable. Understanding these differences is key to effective programming in C.

0 Comment


Sign up or Log in to leave a comment


Recent job openings