Difference between Union and structure of c
What is a structure (struct)?
One way to store 4 different data by creating 4 different arrays for each parameter, such as id[], name[], department[], and email[]. Using array id[i] represents the id of the ith employee. Similarly, name[i] represents the name of ith employee (name). Array element department[i] and email[i] represent the ith employee's department and email address.
The advantage of using a separate array for each parameter is simple if there are only a few parameters. The disadvantage of such logic is that it is quite difficult to manage employee data. Think about a scenario in which you have to deal with 100 or even more parameters associated with one employee. It isn't easy to manage 100 or even more arrays. In such a condition, a struct comes in.
Syntax of struct
Example
What is a Union?
Syntax of Union
Example
Example of Structure and Union difference in C
Example
- C
structure data: integer: 18 decimal: 38.00 name: programmingguru
union data: integer: 18 decimal: 0.00 name: C programming
sizeof structure : 28 sizeof union : 20 Accessing all members at a time:structure data: integer: 183 decimal: 90.00 name: programmingguru
union data: integer: 1801807207 decimal: 277322871721159507258114048.00 name: programmingguru Accessing one member at time: structure data: integer: 240 decimal: 120.000000 name: C programming union data: integer: 240 decimal: 120.000000 name: C programming Altering a member value: structure data: integer: 1218 decimal: 120.00 name: C programming union data: integer: 1218 decimal: 0.00 name: C programming
Difference between Structure and Union
Struct | Union |
---|---|
The struct keyword is used to define a structure. | The union keyword is used to define union. |
When the variables are declared in a structure, the compiler allocates memory to each variables member. The size of a structure is equal or greater to the sum of the sizes of each data member. | When the variable is declared in the union, the compiler allocates memory to the largest size variable member. The size of a union is equal to the size of its largest data member size. |
Each variable member occupied a unique memory space. | Variables members share the memory space of the largest size variable. |
Changing the value of a member will not affect other variables members. | Changing the value of one member will also affect other variables members. |
Each variable member will be assessed at a time. | Only one variable member will be assessed at a time. |
We can initialize multiple variables of a structure at a time. | In union, only the first data member can be initialized. |
All variable members store some value at any point in the program. | Exactly only one data member stores a value at any particular instance in the program. |
The structure allows initializing multiple variable members at once. | Union allows initializing only one variable member at once. |
It is used to store different data type values. | It is used for storing one at a time from different data type values. |
It allows accessing and retrieving any data member at a time. | It allows accessing and retrieving any one data member at a time. |
0 Comments