376 Programming and Data Structures
It is also possible to pass structure elements to a function. This is similar to passing an ordinary
variable to a function. One can pass individual structure elements or entire structure by value or
address.
It is also possible to create structure pointers. In the pointer we have studied pointing a pointer to an
integer, pointing to a float and pointing to a char. In a similar way we can create a pointer pointing to
structure elements. For this it requires - > operator.
12.3 DECLARATION AND INITIALIZATION OF STRUCTURES
Structures can be declared as given below,
struct structtype
{
type variablel;
type variable2;
};
Structure declaration always starts with struct keyword. Here, struet_type is known as tag. The
s true t declaration is enclosed within a pair of curly braces. Using s true t and tag user can declare
structure variables like v aria ble l, variable2 and so on. These are the members of the structure.
After defining structure we can create variables as given below.
struct struct_type vl,v2,v3
Here, vl, v2, and v3 are variables of structure s true t_type. This is similar to declaring variable of any
data type.
int v l, v2,v3
Here, vl, v2, and v3 are variables of integer data type.
The declaration defines the structure but this process doesn't allocate memory. The memory allocation
takes places only when variables are declared.
struct bookl
{
char book[30];
int pages ;
float price ;
};
s true t bookl bk l;
Fig. 12.1 Block diagram of structure
Structure and Union 377
In the above example a structure of type bookl is created. It consists of three members book [30] of
char data type, pages of in t type and price of flo a t data type. Figure 12.1 explains various members
of a structure.
s true t bookl b k l;
The above line creates variable bkl of type bookl and it reserves total 36 bytes (30 bytes for book [30],
2 bytes for integer and 4 bytes for float). Through bkl all the three members of structure can be accessed.
In order to initialize structure elements with certain values following statement is used.
struct bookl bkl = { "shrin ivas",500,385.00};
All the members of structure are related to variable bkl.
s true tu rev a ria ble. member or b k l. book
The period ( . ) sign is used to access the structure members.
We can directly assign values to members as given below
bkl. book ="shrinivas" ;
bkl.pages=500;
b k l.price=385.00;
Few examples are illustrated below for understanding the working of structure.
12.1 Write a program to display size of structure elements. Use sizeof 0 of operator.
mainO
{
struct bookl
{
char book [30] ;
int pages;
float price;
};
struct bookl bkl;
clrscrO ;
printf (" n Size o f Structure Elements");
printf (" n Book : % d",sizeof(bkl.book));
printf (" n Pages: %d",sizeof(bkl.pages));
printf (" n Price: % d ”,sizeof(bkl.price));
printf (" n Total B ytes: % d",sizeof(bkl));
I
O U TPUT;
Size of Structure Elements
Book : 30
Pages: 2
Price : 4
Total Bytes: 36
Explanation In the above program s true ture bookl is defined with three member variables char
book [30] / int pages & flo a t price respectively. Thebkl is an object of the structure bookl.
Using siz eo f () operator their sizes are displayed. The sizes are 30,2 and 4 respectively. The total
size of one record is 36 i.e. size of all member variables of structure.
378 Programming and Data Structures
12.2 Write a program to define a structure and initialize its member variables.
mainQ
{
struct brokl
{
char book[30];
in t pages;
flo a t p rice;
};
struct bookl b k l= {"C++",300,285};
clrscr () ;
printf ("tt Book Name : % s",bkl.book);
printf ("tt No. of Pages :% d",bkl.pages);
printf (" n Book Price : % f',bkl.price);
OUTPUT:
Book Name : C++
No. of Pages :300
Book Price : 285
Explanation In the above program the structure bookl is defined with it's member variables char
book [3 0 ],in t pages and flo a t price. The bkl is an object of the structure bookl. The statement
struct bookl bkl={"C ++," 300,285} defines object b kl and initializes the variables with the values
enclosed in the curly braces respectively. Using p r in t f () statement contents of individual fields are
displayed.
12.3 Write a program to copy structure elements from one object to another object.
# include <stdio.h>
# include <conio.h>
# include <string.h>
void mainQ
{
struct disk
{
char co[15];
flo a t type;
in t price;
};
struct disk dl={"SQNY',,1.44,20};
struct disk d2,d3;
strcpy(d2.co,dl.co);
Structure and Union 379
d2. typdl type;
d2 .price«dl .price;
d3=d2;
clrscrO ;
printf (" n %s %g % d,dl.cojH.type,dl,price);
printf ("n %s %g % d ”fd2.co42.type,d2.price);
printf (" n %s %g % d"fd3.co,d3.type,d3.price);
}
o s im iii
SONY 1.44 20
SONY 1.44 20
SONY 1.44 20
Explanation In the above program dl, d2, and d3 are objects defined based on structure disk. The
object d l is initialized. The contents of d l is copied to d2 and d3 objects. In the first method individual
elements of d l object are copied using assignment statement. The s trcpy () function is used because
the first element of the structure is string. In the second method all the contents d2 are copied to d3.
Here, the statement d3 =d2 performs this task. Thus, in structure it is possible to copy elements of one
object to another object of the same type at one stroke.
12.4 Write a program to read values using scanf () and assign them to structure variables.
xnainO
{
struct bookl
{
char book [30];
in t pages;
flo a t p rice;
};
struct bookl bkl;
clrscrO ;
printf ("Enter Book name, pages, price
scanf ("% s ”, bkl.book);
scanf ("% d , &bkl.pages);
scanf (" % f , &bkl.price);
printf (" n Book Name : %s",bkl.book);
printf (" n No. of Pages: %d",bkl.pages);
printf (" n Book Price : % f f,bkl.price);
I
OUTPUT:
Enter Book name, pages, price :C 500 450
Book Name :C
No. of Pages : 500
Book Price : 450
..................Content has been hidden....................

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