What is difference between union and structure | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is difference between union and structure

?

22nd Jul 2019, 8:26 AM
Aishwarya Viraktamath
Aishwarya Viraktamath - avatar
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)
22nd Jul 2019, 8:59 AM
Kuri
Kuri - avatar
+ 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
22nd Jul 2019, 3:19 PM
Sowmya
+ 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.
23rd Sep 2019, 9:31 AM
A.Sudheer Rajkumar
+ 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.
29th Sep 2019, 11:41 AM
Sankar Rao Mudadla
Sankar Rao Mudadla - avatar
0
Do the C tutorial to find out.
22nd Jul 2019, 9:07 AM
Sonic
Sonic - avatar