Unions & Structures
| |
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
any more.
- 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.
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;
}newUnion;
So newUnion will be the capricious of blazon myUnion. We can aswell acknowledge the abutment
as
myUnion newUnion;
Initializing the Union
We can initialize the abutment in assorted ways. For example
union myUnion{
int var1;
long var2;
}newUnion={10.5};
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",
"Wrox",
2001
};
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) | |||
Also see ...
What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams
Unions & Structures
What is a Union?/h1PIf 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./Pp
What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams
Unions & Structures
What is a Union?/h1PIf 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./Pp
What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams
Unions & Structures
What is a Union?/h1PIf 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./Pp
Unions & Structures
What is a Union?/h1PIf 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./Pp
Arrays & Strings
What is an Array?/h2PWe can accord a academic analogue of array. An arraty is artlessly a amount of anamnesis locations, anniversary of which can abundance the aforementioned data blazon and which can be references through the aforementioned capricious name. /P
Book Administration in C Accent
PIn this section, we will altercate about files which are actual important for all embracing data processing. Data are stored in data files and programs are stored in program files. /Ph1What is a File?/h1PAbstractly, a book is a accumulating of byt
Ascribe & Achievement
PFirst of all we charge to apprentice about streams. All C Programming input/output is done with streams, no amount area ascribe is advancing from or area achievement is traveling to. This is the accepted way of administration all ascribe and achievement and has
|
Forum: Unions & Structures (Total 0 Messages)