Unions & Structures

photoappsios musicappsios racinggamesios
 31 December 18:00   



What is a Union?


If we are accepting the beneath anamnesis to use in our program, for archetype 64K, we


can use a individual anamnesis area for added than one capricious this is alleged union.


You can use the unios in the followig locations.



  1. You can allotment a individual anamnesis area for a capricious myVar1 and use the


    aforementioned area for myVar2 of altered data blazon if myVar1 is not appropriate


    any more.


  3. You can use it if you wish to user, for example, a continued capricious as two


    abbreviate blazon variables.


  5. When you dont understand what blazon of data is to be anesthetized to a function, and


    you canyon abutment which contains all the accessible data types.



Defining a Union


Union can be authentic by the keyword union.


union myUnion{


int var1;


long var2;





Here we accept authentic a abutment with the name myUnion and it has two associates i.e.


var1 of blazon int and var2 of blazon long


Declaring the Union


We can acknowledge the abutment in assorted ways. By demography the aloft archetype we can


acknowledge the aloft authentic abutment as.


union myUnion{


int var1;


long var2;




So newUnion will be the capricious of blazon myUnion. We can aswell acknowledge the abutment




myUnion newUnion;


Initializing the Union


We can initialize the abutment in assorted ways. For example


union myUnion{


int var1;


long var2;





or we can initialize it as


newUnion.var1= 10;


In after stages we can aswell initialize the var2 as able-bodied but this will over


address the var1 value. Commonly if we acknowledge the abutment it is allocated the


anamnesis that its better affiliate can occupy. So actuality in our archetype newUnion will


absorb the anamnesis which a continued blazon capricious can occupy.


What is a Structure


A anatomy is a acceptable apparatus for administration a accumulation of logically accompanying


data items. Anatomy advice to adapt circuitous data is a added allusive way.


It is able abstraction that we may afterwards charge to use in our program Design.



A anatomy is aggregate of altered data types. Lets yield the exmple of


a book, if we deceit to acknowledge a book we will be cerebration about the name , title,


authors and administrator of the book and publishing year. So to acknowledge a book


we charge to accept some circuitous data blazon which can accord with added than one data


types. Lets acknowledge a Book.


Defining a Structure


We can acknowledge a anatomy to board the book.


struct Book




char Name[100];


char Author[100];


char Publisher[80];


int Year;




The keyword struct defines a book, and anniversary band with in the braces defines


the elements of the Book. Now if anytime we make an instance of Book it will


accept all the elements of the anatomy i.e. Name, Author, Administrator and Year.


Declaring a Structure


We can acknowledge the anatomy just like union.


Book CProgrammingBook;



Initializing a Structure


Lets initialize the anatomy capricious CProgrammingBook.


Book CProgrammingBook = 




"Beginning VC++ 6",


"Ivor Horton",








Accessing the associates of a Structure


We can admission all the associates of a anatomy by abacus a aeon afterwards the name


of the anatomy capricious name and then the name of the acreage we wish to access.


For archetype we wish to change the Broadcast year from 2001 to 2002, we will do


it as


CProgrammingBook.Year = 2002;



Discuss this tutorial here



Forum: Unions & Structures (Total 0 Messages)


Tags: types, example, memory, location, members, structures, single, defined, publisher

 union, declare, structure, variable, memory, example, initialize, newunion, occupy, ", publisher, cprogrammingbook, structurewe, myunion{, location, structures, types, defined, unions, members, , declare the, initialize the, structurewe can, var1 long, int var1, data types, myunion{ int, location for, var1 long var2, int var1 long, data types lets, single memory location,

Share Unions & Structures: Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Article In : Computers & Technology  -  C Language Programming