- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Structure and Union
In this post, we will understand the difference between structure and union.
Structure
The ‘struct’ keyword is used to define a structure.
Every member in the structure is provided with a unique memory location.
When the value of one data member is changed, it doesn’t affect other data members in structure.
It helps initialize multiple members at once.
Total size of the structure is equivalent to the sum of the size of every data member.
It is used to store various data types.
It takes memory for every member which is present within the structure.
A member can be retrieved at a time.
It supports flexible arrays.
Following is the syntax of Structure Definition −
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
Union
The ‘union’ keyword is used to define a union.
A memory location is shared by all of its data members.
Changing the value of one data member will affect/change the value of other data members as well.
It allows the user to initialize the first member of union only.
Total size of the union is equivalent to the size of the largest data member in the union.
It is used to store one of the multiple data types available.
It takes memory for a member that has the highest size.
Only one member can be accessed at a time.
It doesn’t support flexible arrays.
Following is the syntax of Union Definition −
union [union tag] { member definition; member definition; ... member definition; } [one or more union variables];
- Related Articles
- Difference between Structure and Union in C
- Difference between Structure and Union in C Program
- State the difference between structure and union with suitable example in C language
- Difference between UNION and UNION ALL in DB2
- Anonymous Union and Structure in C
- Difference between Join and union in SQL
- Difference Between Array and Structure
- Difference Between Structure and Class
- Difference between data type and data structure
- Difference between Structure and Array in C
- Difference between Class and Structure in C#
- What is union of structure in C language?
- Explain the Difference Between Linear and Non-linear Data Structure
- What is the difference between class and structure in Swift?
- Difference Between Direct & Indirect Objects in Sentence Structure
