Structure and Union 381
12.5 Write a program to read and display camumber, starting time and reaching time. Use
structure within structure.
main()
{
struct time
{
int second;
int minute;
int hour;
};
struct t
int carno;
struct time st;
struct time rt;
};
struct tt r l ;
c l r s c r ( ) ;
printf (" n Car No. Starting Time Reaching Time
scanf (" % d " ,&rl.camo);
scanf ( “%d % d %d",&rl.st.hour,&rl.st.minute,&rl.st.second);
scanf ("% d % d %d",&rl.rt.hour,&rl.rt.minute,&rl.rt.second);
printf (" n tC a r No. tStartingTime tReachingTimen");
printf (" t% d t" ,rl.camo);
printf ("t%d:%d:%dtt",rl.sthour,rl.st.minutefrl.st.second);
printf ("t%d:%d:%d",rl.rt.hour,rl.rt.minute,rl.rt.second);
I
Q j j m m
Car No. Starting Time Reaching Time :125 2 50 30 3 50 25
Car No. Starting Time Reaching Time
125 2:50:30 3:50:25
Explanation In the above program two structures are defined. The first structure is time that
contains member fields int second, int minute and int hour. The second structure is t whose member
fields are carno, s t and rt. The variable s t and r t are the objects of the first structure. Using these
two variables it is possible to access the member variables of first structure. The variable r l is an
object of the structure t . The statement r 1. carno accesses the variable carno of the structure t and the
statement r l . s t . hour accesses the variable hour of the structure time. Here, the dot operator is used
twice because we are accessing time structure through object oft structure.