Your SlideShare is downloading. ×
Lecture19 unionsin c.ppt
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Lecture19 unionsin c.ppt


Published on

Published in: Career, Economy & Finance

1 Comment
  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Union Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 2. Define Union●A union, is a collection of variables of differenttypes, just like a structure. However, withunions, you can only store information in onefield at any one time●You can picture a union as like a chunk ofmemory that is used to store variables ofdifferent types.●Once a new value is assigned to a field, theexisting 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 variabletype that can contain many differentvariables (like a structure), but only actuallyholds one of them at a time (not like astructure).●This can save memory if you have a groupof data where only one of the types is usedat a time.●The size of a union is equal to the size ofits largest data or element Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 4. Union Syntaxunion [union-type-name]{ type variable-names; type variable-names; … ...}[union variable];Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 5. Union Exampleunion 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 xLecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 7. Union Example●The compiler will allocate enough storage ina number to accommodate the largestelement in the union. Elements of a unionare accessed in the same manner as astruct.●Unlike a struct, the variables code.m, code.x and code.c occupy the same location inmemory. Thus, writing into one will overwritethe other.Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 8. Union●During accessing, we should make sure that weare accessing the member whose value iscurrently 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 newvalue, the new value supersedes the perviousmember’s value.Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 9. Difference between Structure & Union Structure UnionA structure allocates the total size of allA union only allocates as much memory aselements in it. its largest element (member) requires.Members inside a structure are alwaysThe union will store one and only onestored in separate memory locationsactual value for one element at a time.throughout the lifetime and scope of theentire structure.Manipulations of one member will notIf another element is stored before theaffect the values of any of the others infirst is retrieved, the first stored value isany 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 anstructure union.All data members in a structure are activeOnly on data member is active at a time Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 10. Similarities between Structure & UnionThey both can accept the dot (.) operator to address a member from theobject name, as struct.member or union.memberThey both use brace delimited declarations to create the template for thedata object. Both accept tagname and name as well as explicit initializationas options.They both can have their size correctly determined as maximum size inbytes by use of the sizeof() operator. Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar