C Programming language

adplus-dvertising
WHY USE STRUCTURE
Previous Home Next
WHY USE STRUCTURE

We have seen earlier how ordinary variables can hold one piece of information  and how arrays can hold a number of pieces of information of the  same data type.  These two  data types can handle a great variety of situations. But quite often we deal with entities that are collection of dissimilar data types. For example, suppose  you want to store data about a book. You might want  to store its  name (a string), its price (a float) and number of pages in it (an int). If data about say 3 such books is to be stored, then we can follow two approaches:

  1.  Construct individual arrays, one for storing names, another for storing prices and still another for storing number of   pages.
  2.  Use a structure variable.

Let us examine these two approaches one by one. For the sake of programming convenience assume that the names of books would be single character long. Let us begin with a program that uses arrays.

main( )
{
char  name[3] ;
float  price[3] ;
int  pages[3], i ;
 
printf ( "\nEnter names, prices and no. of pages of 3 books\n" ) ;
 
for ( i = 0 ; i <= 2 ; i++ )
scanf ( "%c %f %d", &name[i], &price[i], &pages[i] );
 
printf ( "\nAnd this is what you entered\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( "%c %f %d\n", name[i], price[i], pages[i] );
}

Output: And here is the sample run...

Enter names, prices and no. of pages of 3 books

A  100.00  354

C  256.50  682

F  233.70  512

And this is what you entered

A  100.000000  354

C  256.500000  682

F  233.700000  512

This approach no doubt allows you to store names, prices and number of pages. But as you must have realized, it is an unwieldy approach that obscures the fact that you are dealing with a group of characteristics related to a single entity—the book.  The program becomes more difficult to handle as the number of items relating to the book go on increasing. For example, we would be required to use a number of arrays, if we also decide to store name of the publisher, date of purchase of book, etc. To solve this problem, C provides a special data type—the structure. A structure contains a number of data types grouped together. These data types may or may not be of the same type. The following example illustrates the use of this data type.

main( )
{
struct book

char  name ;
float  price ;
int  pages ;
} ;
struct book  b1, b2, b3 ;

printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;
scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ;
scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ;

printf ( "\nAnd this is what you entered" ) ;
printf ( "\n%c %f %d", b1.name, b1.price, b1.pages ) ;
printf ( "\n%c %f %d", b2.name, b2.price, b2.pages ) ;
printf ( "\n%c %f %d", b3.name, b3.price, b3.pages ) ;
};

Output: And here is the output...

Enter names, prices and no. of pages of 3 books

A  100.00  354

C  256.50  682

F  233.70  512

And this is what you entered

A  100.000000  354

C  256.500000  682

F  233.700000  512

This program demonstrates two fundamental aspects of structures:

  1. declaration of a structure.
  2. accessing of structure elements.

 

Previous Home Next