Structure and Union 383
Name : Ram Sham Pande
Birth Date : 12.12.1980
Explanation In the above example structure name, b_date and data are defined. The structure
data have member variables of type name and b_date structures respectively. Therefore, this type of
structure is called as nested structure. The variable r l is a variable of type data structure. Using
scan f () statement the program reads data from keyboard. In the same way, using p r in t f ()
statement entered data is displayed on the screen. Here, the dot ( . ) operator is used twice as we
are accessing variables of structure which are inside the another structure.
12.5 ARRAY OF STRUCTURES
As we know array is a collection of similar data types. In the same way we can also define array of
structures. In such type of array every element is of structure type. Array of structures can be declared
as follow.
s tru c t time
{
in t second;
in t minute;
in t hour;
} t [3] ;
In the above example t [3] is an array of 3 elements containing three objects of time structure.
Each element of t [3] has structure of time with 3 members that are second, minute & hour. A
program is explained as given below.
12.7 Write a program to create array of structure objects.
xnainO
{
in t k;
s tr u c t tim e
{
in t second;
In t minute;
in t hour;
>;
s tr u c t t
{
in t carno;
s tru c t tim e s t ;
s tru c t tim e r t ;
struct t t r l [3 ];
c l r s c r ( ) ;
384 Programming and Data Structures
printf ("n C ar No. Starting Time Reaching Time :nrt");
printf C'thhm m:ssthhm m :ss n”);
fo r (k=0;k<3;k++)
I
scanf (" % d " ,& rl[k].cam o);
scanf ( % d % d %d",&rl[k].st.hour,&rl[k].st.minute,& rl[k].st.second);
scanf ("% d %d %d",&rl[k].rt.hour,&rl[k].rt.minute,&rl[k].rt.second);
I
printf ( ntCarNo. tStartingTime tReaching1im en");
fo r (k=0;k<3;k++)
I
printf ( n t% d t" ,rl[k].cam o );
printf ( ut%d:% d:% dtt",rl[k].st.hour,rl[k].st.minute,rl[k].st.second);
p>~*utf ("t%d:%d:%d",rl[k].rt.hour,rl[k].rt.minute,rilkl.rt.second);
I
OUTPUT:
Car No. Starting lim e Reaching Time :
hh:mm:ss hh:mm:ss
120 2 20 25 3 25 58
121
3 25 40
4 40 25
122
4 30 52
5 4010
Car No. Starting Time Reaching Time:
120 2:20:25 3:25:58
121 3:25:40 4:40:25
122 4:30:52 5:40:10
Explanation In the above program two structures time and t are declared. An array of three-
elementsrl [3] isdefined. T h e fir s t fo r loop executes three times and the scan f () statement
reads data through the keyboard for each element of the object. The second f o r loop and the
p r i n t f () statements within it displays the contents of the array of object with their elements.
12.8 Write a program to display names, rollnos, and grades of 3 students who have appeared
in the examination. Declare the structure of name, rollnos and grade. Create an array
of structure objects. Read and display the contents of the array.
# include <stdio.h >
# include <conio.h>
void mainO
{
int k*0;
struct stud
Structure and Union 385
{
char name [12];
in t r o lln o ;
char g r a d e [2 ];
} ;
s tr u c t stud s t [ 3 ] ;
while (k<3)
{ c l r s c r ( ) ;
gotoxy(2,4);
printf ("N a m e : " );
gotoxy(17,4);
scanf ("% s",st[k ],n am e);
gotoxy(2,5);
printf ("R o ll N o . : " );
gotoxy(17,5);
scanf("% d",&st[k].m llno);
gotoxy(2,6);
printf ("G rade :");
gotoxy(l7,6);
scanf ("% s",st[k ].gra de);
st[k ].g rad e[l}='0 ';
puts(" press any key..");
getchO;
k++;
k=0;
clrscrO;
printf (" n N a m et Rollno Graden”);
while (k<3)
{
printf ("n% st% dt% s",st[k ].nam e,st[k ].rollno,st[k].grade);
k++;
I
OUTPUT:
Name Rollno. Grade
Suresh 125 A
Mahesh 126 B
Rajesh 127 A
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.28.179