Structure and Union 389
12.12 Write a program to display the contents of the structure using ordinary pointer.
# include < s tdio .h >
# includ e <conio.h>
vo id m ain()
{
in t *p;
s tru c t num
{
in t a;
in t b;
in t c;
}'•
s tru c t num d;
d .a «2 ;
d.b =3;
d .c *4 ;
pa&d.a;
c l r s c r ( ) ;
printf (" n a=% d”,*p);
printf (" n b= % d ",*(+ + p ));
printf ("n c=% d ",*(+ + p ));
I
OUTPUT;
b«3
Explanation In the above program *p and s tru c tu re num are declared. The structure num has
three members a, b & c of integer data type and initialized with the values 2, 3 and 4 respectively.
We know that structure variables are stored in successive memory locations. If we get starting address
of one variable we can display next elements. The address of variable a is assigned to pointer p. By
applying unary * and ++ operator pointer is increased and values are displayed.
12.7 STRUCTURE AND FUNCTIONS
Like variables of standard data type structure variables also can be passed to the function by value or
address. The syntax of the same is as under.
390 Programming and Data Structures
struct book
{
char name [35] ;
char a u th o r[3 5 ];
in t pages;
} b l ;
void main ()
{
sh o w (& b l);
}
show (s tr u c t book *b2)
{
}
Whenever a structure element is to be passed to any other function, it is essential to declare the
structure outside the main () function i.e. global.
In the above example structure book is declared beforemain ( ) . It is a global structure. Its member
elements are char name [35], char author [35] and in t pages. They can be accessed by all other
functions.
12.13 Write a program to pass address of structure variable to user defined function and
display the contents.
/* passing address of structure variable */
# include <stdio .h>
# include <conio.h>
struct book {
char name [3 5 ];
char author[3 5 ];
in t pages;
>;
void mainO
{
struct book bl= {"JAVA COMPLETE REFERENCE*,*P.NADGHTCar ,886};
show(&bl) ;
>
Structure and Union 391
show (stru ct book *b2)
{
c lr s c r () ;
printf (" n %s by %s of% d pages",b2->name,b2->author,b2->page$);
o i m m i
JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages
Explanation In the above program structure book is defined before main ( ). In the main ()
functionb l is an object declared and initialized. The address of objectbl is passed to function show
(). In the function show () the address is assigned to pointer *b2 that is a pointer to the structure
book. Thus, using - > operator contents of structure elements are displayed.
12.14 Write a program to pass structure elements to function printO and print the elements.
/* p assing s tru c tu re elem ents to fu n ctio n */
# in clu de < s td io.h>
# in clu d e <conio.h>
void main ()
{
struct boy
{
char name [25];
in t age;
in t wt;
} ;
str u c t boy b l = {nA m it",2 0 ,2 5 };
p r in t (b l. n a m e ,b l. a g e ,b l.w t );
}
p r in t (char *s, in t t , in t n)
{
c l r s c r ( ) ;
printf (" n %s %d %d",s,t,n);
return 0;
I
OUTPUT
Amit 20 25
392 Programming and Data Structures
Explanation In the above example structure name has member variables like a character a r r a y
name [2 5 ], age and w eight of integer type. We have passed the base address of name but values
of age and wt. Thus, here values are passed using call by reference and call by value methods.
Instead of passing each element also one can pass entire structure into the function. This is shown in
the given program below.
12.15 Write a program to pass entire structure to user defined function.
I* passing entire structure to function */
# inclu de < s td io .h >
# inclu d e <conio.h>
st r u c t boy
{
char name[2 5 ];
in t age;
in t w t;
v o id m ain()
struct boy bla{*Amit', ,20,25};
print ( b l ) ;
}
p rin t (s tru c t boy b)
{
clrscr();
printf ("tt %s % d %d",b.name,b.age,b.ivt);
return 0;
I
QUJFUT
Amit 20 25
Explanation In the above program s tru c tu re boy is defined outside the main ( ) . So it is
global and every function can access it. The object defined on stru c tu re boy b l is passed to
function prin t ( ) . The formal argument (object) of function p rin t ( ) receives contents of object bl.
Thus, using dot operator contents of individual elements are displayed.
..................Content has been hidden....................

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