Thursday, July 14, 2011

STRUCTURE

STRUCTURE

Structures


Structures are used to group a number of different variables together which enables us treat a number of different variables stored in different place in memory.

Syntax:

struct structure_name
{
Data_type member;
........
};
struct structure_name  structure_variable;

Declaring Structures

A structure is declared by using the keyword struct followed by an optional structure tag followed by the body of the structure. The variables or members of the structure are declared within the body. Here is an example of a structure that would be useful in representing the book containing id, name of book and price.
struct book {
   int id;
   char name[25];
    float price;
};
The struct declaration is a user defined data type. Variables of type book may be declared similarly to the way variables of a built in type are declared.
struct book b1, b2;

Example: Simple example to declare structure and performing input/output process.
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
      int id;
      char name[25];
      float price;
};
struct book b1;
 
printf("Enter the record of book");
scanf("%d %s %f", &b1.id, b1.name, &b1.price);
 
//displaying the book record
printf("%d %s %f", b1.id, b1.name, b1.price);
 
getch();
}

Arrays of Structures

The data structures needed to solve some problems are best represented as an array of structures. Consider, for instance, the problem of storing 100 books record. Here we need to declare book variable like b1, b2, b3 and so on to b100, which is very hard way of doing so. So, this problem can be made easier by using array of structure. Array of structure is declared as follows:
struct book{
    int id;
    char name[25];
    float price;
    };
struct book b[100];
Example: Here is a simple program to illustrate the use of some of these array of structure. This program will input 100 book record and finds out the number of books price above 300.
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
      int id;
      char name[25];
      float price;
};
struct book b[100];
int i, ctr=0;
 
printf("Enter the 100 records of book");
for(i=0; i<100; i++)
      scanf("%d %s %f", &b[i].id, b[i].name, &b[i].price);
 
//finding out the book record whose price is above 300
for(i=0; i<100; i++)
{
if(b[i].price > 300)
      ctr = ctr +1;
}
 
printf("No. of books having price above 300 is %d", ctr);
 
getch();
}

Nested Structure

When a structure is created at first and then again is called by another structure as a member of it. We call this mechanism as nested structure. In another simply can say, structures can contain other structures as members then we call it as nested structure.

 Declaration:

struct Date
{
int dd, mm, yy;
};
struct book
{
int id;
char name[25];
float price;
struct Date issue;
};
struct book b;

Example:

    #include<stdio.h>
    void main()
    {
          struct Date
          {
                int dd, mm, yy;              
          };
          struct student
          {
                int roll;
                char name[25]
                struct Date dob;       
          };
          struct student s;
         
          //taking input in normal structure
          scanf("%d %s",&s.roll, name);
          //taking input in nested structure
          scanf("%d %d %d",&s.dob.dd, &s.dob.mm, &s.dob.yy);
         
          //Displaying the value of structure
          printf("%d %s",s.roll, name);
          //Displaying the value of nested structure
          printf("%d %d %d",s.dob.dd, s.dob.mm, s.dob.yy);
         
          getch();      
    }

0 comments:

Post a Comment