×

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure or Union overcome this problem. Structure or Union have used to store the mixed type of data. That is, the Structure or Union is a group of mix data type which are used separately.

Structure

The structure is a user-defined data-type in the data structure that allows the combination of various types of the data-type. It is used to describe a record. It is represented by the struct keyword in the program.

To define the structure, it uses the struct structure_name statement, as shown in syntax.

Syntax

 struct structure_name
    {   
data-type member 1;   
data-type member 2;   …   
data-type member n;   
};        

Note: struct structure_name is used to define the name of the structure and data-type member defines the data-type.

Advantages of Structure

  1. It can store different types of the data-type.
  2. It is user-friendly and simple to understand.
  3. It requires less time to write in the program.

Program

#include<stdio.h>
#include<conio.h>
void main( ) 
{   
struct Teacher 
{   
int teacher_id ; 
float salary ; 
int mobile_no ; 
}   ; 
struct Teacher t1,t2,t3; 
clrscr();   
printf("\nEnter the teacher_id, salary & mobile_no of the teacher\n"); 
scanf("%d %f %d", &t1.teacher_id, &t1.salary,   &t1.mobile_no); 
scanf("%d%f %d", &t2.teacher_id, &t2.salary, &t2.mobile_no);   
scanf("%d %f %d", &t3.teacher_id, &t3.salary,   &t3.mobile_no); 
printf("\n Entered Result "); 
printf("\n%d %f %d", t1.teacher_id, t1.salary, t1.mobile_no); 
printf("\n%d%f %d", t2.teacher_id, t2.salary, t2.mobile_no); 
printf("\n%d %f %d", t3.teacher_id, t3.salary, t3.mobile_no); 
getch();                                                                
  }    

Union

The Union is a user-defined data type in the data structure that allows different data types to be stored in the same memory location. It is represented by the union keyword in the program. A union of several members may be defined together, but at any time, only one member can be valued. Union provides an effective way to use the same memory space for multiple purposes.

To define the Union, it uses the union union_name statement, as shown in syntax.

Syntax

union union_name   
{   
data-type member 1;  
data-type member 2;   …   
data-type member n;  
 };  

Note: union union_name is used to define the name of the Union and data-type member defines the data-type.

Union works like structure while making the program of the Union, union is written instead of struct keyword.

Program

#include<stdio.h>
#include<conio.h>
void main( )
\
 {  
 union Teacher 
{   
int teacher_id ;
 float salary ;
 int mobile_no ; 
} ;
 union Teacher t1,t2,t3; 
clrscr();   
printf("\nEnter the teacher_id, salary & mobile_no of the teacher\n"); 
scanf("%d %f %d", &t1.teacher_id, &t1.salary,   &t1.mobile_no); 
scanf("%d%f %d", &t2.teacher_id, &t2.salary, &t2.mobile_no);   
scanf("%d %f %d", &t3.teacher_id, &t3.salary,   &t3.mobile_no);
printf("\n Entered Result "); 
printf("\n%d %f %d", t1.teacher_id, t1.salary, t1.mobile_no); 
printf("\n%d%f %d", t2.teacher_id, t2.salary, t2.mobile_no); 
printf("\n%d %f %d", t3.teacher_id, t3.salary, t3.mobile_no); 
getch();                                                               
   }    

Advantages of Union

  1. It takes the less memory space than the structure.
  2. It is used when two or more data member has to use the same memory place.

Difference between structure and union

Structure Union
It is represented by the struct keyword in the program. It is represented by the union keyword in the program.
In the structure, every member is assigned a separate memory location. In the Union, every member is shared the same memory location.
The memory size of the structure is equal to the sum of the memory size of each data-type member. The memory size of the union is equal to the largest size of all data-type in the union.
It supports a flexible array. It does not support a flexible array.
In the structure, it can be stored multiple values of the different data members at a time. In the union, it can be stored one value at a time.
Syntax  struct structure_name   {     data-type member 1;     data-type member 2;     …     data-type member n;    };   Syntax    union union_name   {     data-type member 1;     data-type member 2;     …     data-type member n;    };  

Related Topics

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Strings in Data Structures

Strings and functions in C A string is a collection of characters. We'll learn how to declare strings, operate with strings in C programming, and use pre-defined string handling routines. We'll look...

7 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

Bitwise Operators and their Important Tricks

In most of the programs you write today, you deal with data types comprising bytes, such as integer, float, double, etc. Dealing with bytes? It is a quite normal task,...

5 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Stack vs Heap Memory Allocation Data Structure

Difference Between Stack and Heap Memory Allocation Stack Memory Stack memory allocation is a way to use the system memory as a temporary storage of the data which is act like last-in-first-out...

3 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Operations of B Tree in C++ Language

B tree tends to be a self-aligning and balancing tree that helps us organise our data and document safely. We know that every data or information in the B tree...

9 minutes read.

Create a binary search tree

Implementation In this section of the article, we will see the usage and mechanism of how we will create a given binary tree. Let's observe these in more depth and then...

7 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.