Lecture19 unionsin c.ppt
description
Transcript of Lecture19 unionsin c.ppt
Prakash Khaire, LecturerB V Patel Inst. of BMC & IT, Gopal
Vidyanagar
Union
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
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
●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
Union
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union Syntax
union [union-type-name] {
type variable-names;type variable-names;
… ... }[union variable];
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union Example
union item{
int m;float x;char c;
} code;
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Sharing of a storage locating by union members
1000 1001 1002 1004
c
x
m
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
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
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 errorprintf(“%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
Difference between Structure & UnionStructure Union
A structure allocates the total size of all elements in it.
A union only allocates as much memory as its largest element (member) requires.
Members inside a structure are always stored in separate memory locations throughout the lifetime and scope of the entire structure.
The union will store one and only one actual value for one element at a time.
Manipulations of one member will not affect the values of any of the others in any way unless they are operated on in code to do so.
If another element is stored before the first is retrieved, the first stored value is lost.
The keyword struct is used to declare a structure
The keyword union is used to declare an union.
All data members in a structure are active at time
Only on data member is active at a time.
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
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