Structures Introduction
Why do we use structures?
Ordinary variables may contain one piece of information and the way arrays may contain a number of pieces of information of the same type of data. Both types of data are able to handle a wide variety of situations. But quite often we deal with entities that are collections of dissimilar data types.
Definition:
A structure is a set of heterogeneous data elements that are stored in consecutive memory places.
The structure is classified as user-defined data types.
For instance, let’s say we want to store data on a book You might want to store its name(a string), its price (a float), and the number of pages in it (an int). If data about saying 3 such books must be stored, then we can take two approaches:
(a) Construct individual arrays, one to store names, another to store prices, and yet another to store a certain number of pages.
(b) Use a structure variable.
To examine these two approaches one by one..let us begin with a program that uses Arrays.
#include<stdio.h>
void 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] );
}
This approach no doubt allows you to store names, prices, and the 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 goes on increasing. For example, we would be required to use a number of arrays, if we also decide to store the name of the publisher, date of purchase of the book, etc. To solve this problem, C provides a special data type—the structure.
Defining Structure:
Syntax of structure declaration:
struct structure name
{
datatype1 var1,var2,…;
datatype2 var1,var2,…;
……………
datatypen var1,var2,…;
};
- The struct keyword is used to declare structures.
• The keyword struct identifies the beginning of a structure definition.
• It’s followed by a structure name or tag that is the name given to the structure.
• Following the tag are the structure members, enclosed in braces.
• A structure can contain any of C’s data types, including arrays and other structures.
• Each variable within a structure is called a member of the structure.
• The above structure declaration is also known as a template.
• The template is terminated with a semicolon (;).
Examples:
1.Define a structure by name student which includes student name, roll number, and marks.
struct student
{
char name [10];
int rno;
float marks ;
} ;
This statement defines a new data type called struct student.
2. Define a structure by name book which includes the name of the book, price, and the number of pages.
struct book
{
char name ;
float price ;
int pages ;} ;
This statement defines a new data type called struct book.
