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.
- 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
- You can use it if you wish to user, for example, a continued capricious as two
abbreviate blazon variables.
- 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.
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.
So newUnion will be the capricious of blazon myUnion. We can aswell acknowledge the abutment
Initializing the Union
We can initialize the abutment in assorted ways. For example
or we can initialize it as
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.
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.
Initializing a Structure
Lets initialize the anatomy capricious CProgrammingBook.
Book CProgrammingBook =
"Beginning VC++ 6",
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
CProgrammingBook.Year = 2002;
Discuss this tutorial here
|Forum: Unions & Structures (Total 0 Messages)|
Also see ...Permalink
Article In : Computers & Technology - C Language Programming