Structure in C

A structure contains a number of data types grouped together. These data types may or may not be of the same type

Uses of Structures in C

Where are structures useful? The immediate application that comes to the mind is Database Management. That is, to maintain data about employees in an organization, books in a library, items in a store, financial accounting transactions in a company etc. But mind you, use of structures stretches much beyond database management. They can be used for a variety of purposes like:

  • Changing the size of the cursor
  • Clearing the contents of the screen
  • Placing the cursor at an appropriate position on screen
  • Drawing any graphics shape on the screen\
  • Receiving a key from the keyboard
  • Checking the memory size of the computer
  • Finding out the list of equipment attached to the computer
  • Formatting a floppy
  • Hiding a file from the directory
  • Displaying the directory of a disk\
  • Sending the output to printer
  • Interacting with the mouse

Example of structure in C

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 of Structure in C

Enter names, prices and no. of pages of 3 books
A 100.00 354
C 256.50 682
F 233.70 512

Declaring a Structure

struct book
{
char name ;
float price ;
int pages ;
} ;

This statement defines a new data type called struct book. Each variable of this data type will consist of a character variable called name, a float variable called price and an integer variable called pages

The general form of a structure declaration statement

struct
{
structure element 1 ;
structure element 2 ;
structure element 3 ;
……
……
} ;

Once the new structure data type has been defined one or more variables can be declared to be of that type. For example the variables b1, b2, b3 can be declared to be of the type struct book, as,

struct book b1, b2, b3 ;

This statement sets aside space in memory. It makes available space to hold all the elements in the structure—in this case, 7 bytes—one for name, four for price and two for pages. These bytes are always in adjacent memory locations.

For example

struct book
{
char name ;
float price ;
int pages ;
} ;
struct book b1, b2, b3 ;
is same as…
struct book
{
char name ;
float price ;
int pages ;
} b1, b2, b3 ;
or even…
struct
{
char name ;
float price ;
int pages ;
} b1, b2, b3 ;

Note the following points while declaring a structure type:

  • The closing brace in the structure type declaration must be followed by a semicolon
  • It is important to understand that a structure type declaration does not tell the compiler to reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the structure
  • Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined. In very large programs they are usually put in a separate header file, and the file is included (using the preprocessor directive #include) in whichever program we want to use this structure type.

Accessing Structure Elements

In arrays we can access individual elements of an array using a subscript. Structures use a different scheme. They use a dot (.) operator. So to refer to pages of the structure defined in our sample program we have to use

b1.pages

Array of Structures

Usage of an array of structures

To store data of 100 books we would be required to use 100 different structure variables from b1 to b100, which is definitely not very convenient. A better approach would be to use an array of structures

main( ) 
{ 
 struct book 
 { 
 char name ; 
 float price ; 
 int pages ; 
 } ; 
 struct book b[100] ; 
 int i ; 
 for ( i = 0 ; i <= 99 ; i++ ) 
 { 
 printf ( "\nEnter name, price and pages " ) ; 
 scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ; 
 } 
 for ( i = 0 ; i <= 99 ; i++ ) 
 printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages ) ; 
} 
linkfloat( ) 
{ 
 float a = 0, *b ; 
 b = &a ; /* cause emulator to be linked */ 
 a = *b ; /* suppress the warning - variable not used */ 
}

Leave a Comment