1. Union
Prakash Khaire, Lecturer
B V Patel Inst. of BMC & IT, Gopal
Vidyanagar
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
2. Define Union
●A union, is a collection of variables of different
types, just like a structure. However, with
unions, you can only store information in one
field at any one time
●You can picture a union as like a chunk of
memory that is used to store variables of
different types.
●Once a new value is assigned to a field, the
existing data is wiped over with the new data.
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
3. Union
●A union can also be viewed as a variable
type that can contain many different
variables (like a structure), but only actually
holds one of them at a time (not like a
structure).
●This can save memory if you have a group
of data where only one of the types is used
at a time.
●The size of a union is equal to the size of
it's largest data or element
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
4. Union Syntax
union [union-type-name]
{
type variable-names;
type variable-names;
…
...
}[union variable];
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
5. Union Example
union item
{
int m;
float x;
char c;
} code;
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
6. Sharing of a storage locating by union
members
1000 1001 1002 1004
c
m
x
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
7. Union Example
●The compiler will allocate enough storage in
a number to accommodate the largest
element in the union. Elements of a union
are accessed in the same manner as a
struct.
●Unlike a struct, the variables code.m, code.
x and code.c occupy the same location in
memory. Thus, writing into one will overwrite
the other.
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
8. Union
●During accessing, we should make sure that we
are accessing the member whose value is
currently stored.
code.m = 123;
code.x = 23.5;
printf(“%d”,code.m); //this will generate error
printf(“%d”,code.x);
●When a different member is assigned a new
value, the new value supersedes the pervious
member’s value.
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
9. Difference between Structure & Union
Structure Union
A structure allocates the total size of allA union only allocates as much memory as
elements in it. its largest element (member) requires.
Members inside a structure are alwaysThe union will store one and only one
stored in separate memory locationsactual value for one element at a time.
throughout the lifetime and scope of the
entire structure.
Manipulations of one member will notIf another element is stored before the
affect the values of any of the others infirst is retrieved, the first stored value is
any way unless they are operated on inlost.
code to do so.
The keyword struct is used to declare a The keyword union is used to declare an
structure union.
All data members in a structure are activeOnly on data member is active at a time.
at time
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
10. Similarities between Structure & Union
They both can accept the dot (.) operator to address a member from the
object name, as struct.member or union.member
They both use brace delimited declarations to create the template for the
data object. Both accept tagname and name as well as explicit initialization
as options.
They both can have their size correctly determined as maximum size in
bytes by use of the sizeof() operator.
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar