380 Programming and Data Structures
Explanation This program is the same as the previous one. Instead of initializing values, the values
are read using scanf () statements and structure variables. T h ep rin tf () statement displays the
contents of structure variables.
12.4 STRUCTURE WITHIN STRUCTURE
We can take any data type for declaring structure members like in t, flo a t, char etc. In the same way
we can also take object of one structure as member in another structure. Thus, structure within structure
can be used to create complex data applications. The syntax of the structure within the structure is as
follows.
struct time
{
in t second;
in t minute;
in t hour;
};
struct t
{
in t carno;
struct time st;
struct time et;
};
struct t fla y e r ;
Figure 12.2 Structure within structure
Few examples are illustrated below for understanding
player
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.
382 Programming and Data Structures
12.6 Write a program to enter full name and date of birth of a person and display the same*
Use nested structure.
# include <stdio.h >
# include <conio.h>
void mainO
{
struct name
{
char f i r s t [1 0 ];
char second[1 0 ];
char la s t [10] ;
struct b_date
{
in t day;
int month;
int year;
}>
struct data
{
struct name nm;
struct b_date bt;
h
struct data r l;
clrscrO;
printf (" n Enter Name ( First / Second / Last ) n ");
scanf ("% s % s %s",rl.nm.first,rl.nm.second,rl.nm.last);
printf (" n Enter Birth Date D ay / M onth / Yearn");
scanf ("% d % d % d ,&rl.bt.day,&rl'bt.month,&rl.bt.year);
printf ("N a m e : %s %s %sn",rl.nm.first,rl.nm.second,rl.nm.last);
printf ("Birth Date : %d.%d.%d",rl.bt.day,rl.bt.month,rl.bt.year);
OUTPUT:
Enter Name ( First / Second / Last )
Ram Sham Pande
Enter Birth Date Day / Month / Year
12 12 1980
..................Content has been hidden....................

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