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
+ 1

What is difference between union and structure?

14th Apr 2018, 4:34 PM
Puneet Bhardwaj
Puneet Bhardwaj - avatar
3 Answers
+ 3
Structure is a collection of different types of elements on the single name
14th Apr 2018, 4:47 PM
Jayesh Patil
Jayesh Patil - avatar
+ 3
for example take a structure and union in the following way struct { int a; int b; }c; int main() { /* scaning a and b structure uses different memory locations for a and b so after scaning we can print them in any way*/ cin>>c.a; cin>>c.b; cout<<c.a; cout<<c.b; } if input: 2 3 output:2 3 union { int c; int d; }e; int main() { /*in union it uses same memory location for all the variables in it */ cin>>e.c; cout<<e.c; cin>>e.d; cout<<e.d; /* here in unions we should print or use the value immediately other wise the value will be replaced with the next value*/ } ## if my solution is acceptable reply me with an vote###
14th Apr 2018, 4:57 PM
Tirumalesh
Tirumalesh - avatar
+ 1
hii
14th Apr 2018, 4:35 PM
Kavakarine