0
What is difference between union and structure
?
5 Answers
+ 5
Both are derived data types.
Size of structure depends on size of all elements.
While, size of union depends on the size of Single element which has maximum size.
Ex: struct sexample {
int A;
int B;
char C;
} - > size is 9 (4+4+1) bytes.
union uexample {
int A;
int B;
char C;
} - > size is 4 bytes (size of int)
+ 4
Structure uses different memory location for its elements whereas union uses a single memory location for its elements the elements are overwritten in the same location
+ 1
Sizeof structure is the size of its individual members but whereas in Union.., size of union is the highest data type present in it.
Structure has different memory locations for each of its member but Union shares the memory location with other members where previous value is replaced.
+ 1
Structure is a user defined data type. The size of the structure is the sum of all the data types which are declared inside the block like float char or int.....
It can access all the datatypes at a time.
Example:-
Student details.
Like his name is stored in char and his marks are stored in int and his percentage is stored in float.
I.e.., the sum of all the float, char and int is occupied in structure.
But, where as in union. It holds only the highest size of all the data types in the block. It only access one data type at a time. So no need to store the all sizes.
0
Do the C tutorial to find out.