386 Programming and Data Structures
Explanation In the above program structure stud is declared with it's members char name
[1 2], in t ro lln o and char grade [2 ]. The array st [3] of structure stud is declared. The
f i r s t while loop and scanf () statements within the loop are used for repetitive data reading.
The second w hile loop and p rin tf () statements within it displays the contents of an array.
12.6 POINTER TO STRUCTURE
We know that pointer is a variable that holds the address of another data variable. The variable may
be of any data type i.e. int, flo a t or double. In the same way we can also define pointer to structure.
Here, starting address of the member variables can be accessed. Thus, such pointers are called structure
pointers.
Exam ple
struct book
{ char name[25 ];
char author[2 5 ];
in t pages;
j*
struct book *p tr;
In the above example *ptr is pointer to structure book. The syntax for using pointer with member
is as given below.
1) ptr->name 2) ptr->author 3) p tr- >pages.
By executing these three statements starting address of each member can be estimated.
The below given programs illustrate the use of pointer to structure.
12.9 Write a program to declare pointer to structure and display the contents of the structure.
# include <stdio.h >
# include <conio.h>
void main()
{
struct book
{ char name[2 5];
char autho r[2 5 ];
in t pages;
} ;
stru ct book b l-{"JA V A COMPLETE REFERENCE", "P .NAUGHTON", 886};
struct book *p tr;
ptr*&bl;
clrscr ();
printf ("n %s by %s of %dpages",bl.name,bl.author,bl.pages);
printf (" n %s by %s o f% d pages", ptr->name,ptr->author,ptr->pages);
Structure and Union 387
OUTPUT;
JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages
JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages
Explanation In the above program the function p rin t f () statement prints structure elements by
calling them as usual. In the second p rin t f () statement to print the structure elements using pointer
an arrow operator (- and > togetner) is used instead of dot ( . ) operator. The reason is that the p tr is
not a structure variable but poin ter to a structure.
12.10 Write a program to declare pointer as members of structure and display the contents
of the structure.
# in clu d e < std io .h >
# in clu d e <con io.h>
v o id mainO
{
s tru c t boy
{
char *name;
in t *age;
flo a t *height;
};
static struct boy *sp;
char nm[1 0 ]="Mahesh";
in t ag=20;
f lo a t ht=5.40;
sp- >name=mn;
sp->age=&ag;
sp->h eigh t=& ht;
c l r s c r ( ) ;
p rin tf ("n Name = % s”,sp->nam e);
p rin tf (" n Age = % d *sp->age);
p rin tf C'n H eight = % f,*sp->height);
I ' ' '
OUTPUT:
Name = Mahesh
Age = 20
Height = 5.400000
388 Programming and Data Structures
Explanation In the above program stru c tu re boy is declared. The members of s tru ctu re
boy are pointers. The pointer *sp is pointer to stru c tu re boy. Another three ordinary variables
char nm [10] ="Mahesh", in t ag=20 and f lo a t h t=5 .40 are declared and initialized. The
addresses of these ordinary variables are assigned to structure variables using arrow operator. Using
p r in t f () statement the contents of structure variables are displayed.
12.11 Write a program to declare pointer as members of structure and display the contents
of the structure without using -> (arrow) operator.
# include < std io .h >
# in clu de <con io.h>
# in clu d e < s trin g .h >
v o id main()
{
s tru c t boy
{
char *name;
in t *age;
flo a t * h e ig h t;
} ;
s tru c t boy b;
char nm[1 0 ]="Somesh";
in t ag=20;
f lo a t h t=5 .40;
s t r c p y (b . name, nm);
b . age=&ag;
b.h e igh t= & h t;
c l r s c r ( ) ;
printf (" n Name = %s",b.name);
printf (" n Age = %d",*b.age);
printf (" n Height = %g",*b,height);
}
Q u m r r ;
Name = Somesh
Age = 20
Height = 5.4
Explanation This program is same as the previous one. Here no pointer is declared in structure.
Hence, using dot operator we can display the contents of the structure.
..................Content has been hidden....................

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