stru union
TRANSCRIPT
-
8/12/2019 Stru Union
1/11
A structure is a user defined data type. We know that arrays can be
used to represent a group of data items that belong to the same type, such as int
or float. However we cannot use an array if we want to represent a collection of
data items of different types using a single name. A structure is a convenient tool
for handling a group of logically related data items.
The syntax of structure declaration is
struct structure_name
type element !"
type element #"
$$$$$..
type element n"
%"
&n structure declaration the keyword struct appears first, this
followed by structure name. The member of structure should be enclosed
between a pair of braces and it defines one by one each ending with a semicolon.
&t can also be array of structure. There is an enclosing brace at the end of
declaration and it end with a semicolon.
We can declare structure variables as follows
struct structure_name var!,var#,$..,var n"
'or (xample)
To store the names, roll number and total mark of a student you
can declare * variables. To store this data for more than one student * separate
arrays may be declared. Another choice is to make a structure. +o memory is
allocated when a structure is declared. &t ust defines the -form of the structure.
When a variable is made then memory is allocated. This is e/uivalent to saying
that there0s no memory for -int , but when we declare an integer that is. int var"
only then memory is allocated. The structure for the above1mentioned case will
look like
-
8/12/2019 Stru Union
2/11
struct student
int rollno"
char name2#34"
float totalmark"
%"
We can now declare structure variables stud!, stud# as follows
struct student stud!,stud#"
Thus, the stud! and stud# are structure variables of type student. The above
structure can hold information of # students.
&t is possible to combine the declaration of structure combinationwith that of the structure variables, as shown below.
struct structure_name
type element !"
type element #"
$$$$$..
type element n"
%var!,var#,$,varn"
The following single declaration is e/uivalent to the two
declaration presented in the previous example.
struct student
int rollno"
char name2#34"
float totalmark" % stud!, stud#"
The different variable types stored in a structure are called its
members. The structure member can be accessed by using a dot 5.6 operator, so
the dot operator is known as structure member operator.
-
8/12/2019 Stru Union
3/11
(xample)
&n the above example stud! is a structure variable of type
student. To access the member name, we would write
stud!.name
7imilarly, stud!8s rollno and stud!8s totalmark can be accessed by writing
stud!.rollno And
stud!.totalmark
Initializing Structure Members
7tructure members can be initiali9ed at declaration. This much the same manner
as the element of an array" the initial value must appear in the order in which
they will be assigned to their corresponding structure members,enclosed inbraces and seperated by commas .The general form is
struct stucture_name var:val!,val#,val*$..%"
(xample)
;include
-
8/12/2019 Stru Union
4/11
printf5?nBoll number)Idnn+ame)IsnnTotel mark)
I.#fn?,stud#.rollno,stud#.name,stud#.totalmark6"
printf5?nBoll number)Idnn+ame)IsnnTotel mark)
I.#fn?,stud*.rollno,stud*.name,stud*.totalmark6"
getch56"
return J"
%
Array of structures:
&t is possible to store a structure has an array element. i.e., an array in
which each element is a structure. Kust as arrays of any basic type of variable are
allowed, so are arrays of a given type of structure. Although a structure containsmany different types, the compiler never gets to know this information because it
is hidden away inside a sealed structure capsule, so it can believe that all the
elements in the array have the same type, even though that type is itself made up
of lots of different types.
The declaration statement is given below.
struct struct_name
type element !"
type element #"
$$$$$..
type element n"
%array name2si9e4"
(xample)
struct student
int rollno"
char name2#34"
float totalmark"
% stud2!JJ4"
-
8/12/2019 Stru Union
5/11
&n this declaration stud is a !JJ1element array of structures.
Hence, each element of stud is a separate structure of type student. An array of
structure can be assigned initial values ust as any other array. 7o the above
structure can hold information of !JJ students.
program)
;include
-
8/12/2019 Stru Union
6/11
printf5?nBoll number)Idn?,stud2i4.rollno6"
printf5?+ame)Isn?,stud2i4.name6"
printf5?Totel mark)Idn?,stud2i4.totalmark6"
%
getch56"
return J"
%
Structure as structure member:
A structure can have one or more of its member as another
structure, but a structure cannot be member to itself when a structure is used as
structure member. &n such situation, the declaration of the embedded structuremust appear before the declaration of the outer structure. 'or example
;include
-
8/12/2019 Stru Union
7/11
printf5?(nter total number of studentsnn?6"
scanf5?Id?,Ln6"
for(i=0;i
-
8/12/2019 Stru Union
8/11
The real purpose of unions is to prevent memory fragmentation by
arranging for a standard si9e for data in the memory. Oy having a standard data
si9e we can guarantee that any hole left when dynamically allocated memory is
freed will always be reusable by another instance of the same type of union. This
is a natural strategy in system programming where many instances of different
kinds of variables with a related purpose and stored dynamically.
A union is declared in the same way as a structure.The syntax of
union declaration is
union union_name
type element !"
type element #"
$$$$$..
type element n"
%"
This declares a type template. Dariables are then declared as)
union union_name x,y,9"
'or example, the following code declares a union data type called
7tudent and a union variable called stud)union student
int rollno"
float totalmark"
%"
union student stud"
&t is possible to combine the declaration of union combinationwith that of the union variables, as shown below.
union union_name
type element !"
type element #"
-
8/12/2019 Stru Union
9/11
$$$$$..
type element n"
%var!,var#,$,varn"
The following single declaration is e/uivalent to the two
declaration presented in the previous example.
union student
int rollno"
float totalmark"
%x,y,9"
(xercise) Pompare structure and Enion
7tructure)
-
8/12/2019 Stru Union
10/11
Enion)
#include
#includeint main()
{
union student {
char *name;
int rollno;
float totalmark; }stud1;
// union student stud1={"shraf"!1!}; stud1.name="shraf"; $rintf("%&'&% &+,%-nname-s"!stud1.name);
stud1.rollno=1; $rintf("n0oll numer-d"!stud1.rollno);
stud1.totalmark=23.45;
-
8/12/2019 Stru Union
11/11
$rintf("ntotalmark-f"!stud1.totalmark);
6hat is the main difference et7een %&0'8&'0 and '+9:
All the members of the structure can be accessedatonce,where as in an union only one member can be usedat a time.Another important difference is in the size allocatedto astructure and an union.for eg:
struct example{ int integer; float floating_numbers;}the size allocated here is sizeofint!"sizeoffloat!;where as in an unionunion example{ int integer;
float floating_numbers;}size allocated is the size of the highest member.so size is#sizeoffloat!;