What is the difference between structure and union? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 21

What is the difference between structure and union?

14th Nov 2016, 4:32 PM
Chandrakant More
Chandrakant More - avatar
8 Respuestas
+ 14
A structure is a class, basically. Or if we go a bit more oldschool (C), it's simply a collection of values. This example struct { int a; int b; } s; s.a = 4; s.b = 3; cout << s.a << ", " << s.b << endl; will output "4, 3". So far so obvious! Unions are different, in that all the members of a union occupy the same place in memory. That means if we change our struct to union { int a; int b; } s; and run the above code again, we will see "3, 3". `a` and `b` point to the same place in memory, so changing one will change the other. This might seem a bit pointless and a union of two ints probably is, but there are a few use cases. If you are just starting out I don't recommend using unions and they're mostly a C thing so you probably won't need them in C++ at all, but here's an example: union { int coordinates[2]; struct { int x; int y; }; } s; s.x = 4; cout << s.coordinates[0] << endl; This will print "4"! The struct and the array point to the same place in memory, so this works (at least on most systems). I guess it's good to have heard about unions, but they aren't the most important thing in the world. You usually use them when dealing with rather low-level and exotic stuff, so don't worry about them too much. You'll need to have a firm grasp on pointers before fully getting what's going on I think :)
14th Nov 2016, 5:19 PM
Schindlabua
Schindlabua - avatar
+ 13
Thank you dude
15th Nov 2016, 8:47 PM
Chandrakant More
Chandrakant More - avatar
+ 8
Structure can share memory has a one block only but union share has various block in one memory block
25th Dec 2018, 10:57 AM
Grandhi priyanka
Grandhi priyanka - avatar
+ 5
alguien habla español?
12th May 2018, 9:04 PM
Romina Anabel
Romina Anabel - avatar
+ 3
A union allows to store different data types in the same memory location. It is like a structure because it has members. However, a union variable uses the same memory location for all its member's and only one member at a time can occupy the. A union declaration uses the keyword union, a union tag, and curly braces { } with a list of members. Union members can be of any data type, including basic types, strings, arrays, pointers, and structures.
3rd Jan 2019, 12:00 AM
Lloyd L Conley
Lloyd L Conley - avatar
+ 2
In a language like COBOL a structure is called a record while a union is accomplished with the REDEFINES keyword making it more obvious that the structure contains contiguous data fields (variables) while unions contain overlapping variables. In a table format it would look like this: STRUCTURE Start length. Variable 1. 1. Char 2. 5. Int But a UNION would look like this: Start length variable 1. 1. Char 1. 4. Int. Putting 'x' in the char and 1 in the int would result in the char containing 'x' in the struct, but would change its value in the union.
27th Jan 2019, 4:14 PM
Roger Greenlaw
+ 2
In the structure all variables occupied the some bite of memory but in union the memory will be execute only big size of variable and all other rest is joint them.
31st Mar 2019, 3:29 PM
Deepak Sharma
Deepak Sharma - avatar
0
Wt about size of the both
10th Jan 2019, 2:10 AM
Thaslima
Thaslima - avatar