Pages

January 23, 2013

Structure in C-language


implementation of structures in programming from day to day life

Structure is the user defined data type which can store heterogeneous data i.e. data of different types. It means in structure member with all possible data type can be used and there is no restriction that elements of the structure are stored at adjacent memory locations. Structure is declared with key word struct followed by its name and then by block. 


Below structure it can be referred by new name and format is:
struct name  
{
   int num1;
   double num2;
   float num3;
 };
void main()
{
   ……………;
  struct name n;
   …………….;
  }

The members of the structure are accessed by using structure name.member name
Example: n.num1, n.num2, n.num3 etc.
It means during input or output calculation this format should be followed strictly.

SUB-SCRIPTED STRUCTURE

In this structure whenever same procedure has to repeat again and again then the reference of the structure should have a subscript name along with the structure name and this sub-scripted variable is used in square brackets []. It means tag of the array should be 1-D.

Format of sub-scripted structure is:
struct name
{
   int st1;
   float st2;
   char st3;
 };
void main()
{
   ………….;
   struct name nm[100]; 
   ………….;
   (rest it is as 1-D array);
   …………..;
  }

It means one for loop is required to store numbers in sub-scripted array and other for loop is required to print such a structure and for processing.

/*Program to enter students data which consist of name, roll number, age and class and print it in sequence*/

#include<stdio.h>
#include<conio.h>
struct stu
{
       int rno,age;
       char name[20],clas[10];
       };
void main()
{
     int i,n;
     struct stu s[100];
     printf("\n enter how much students data you want to enter");
     scanf("%d",&n);
     for(i=0;i<n;i++)
     {
            printf("\n enter rollno,name,age,class");
            scanf("%d%s%d%s",&s[i].rno, s[i].name, &s[i].age, s[i].clas);
            }
      
      printf("\n rno \tname \tage \tclass");
      for(i=0;i<n;i++)
      {
                      printf("\n %d \t%s \t%d \t%s", s[i].rno, s[i].name,s[i].age,s[i].clas);
                      printf("\n");
                      }
       getch();
       }         
output of program to show number of student details in tabular form using structures in C language

NESTED STRUCTURE

When any structure is used with in another structure by declaring it as member of the structure then the structure is said to be nested into other structure.
It means just like other members i.e. variable array. Any structure can also be used as member of the structure. The reference is given to the inner 1st structure inside another structure. 

Format is:
struct  name1
{
   int num1;
   ……………..;
  };
struct  name2
{
   int num2;
   ………………;
  }
struct name1 n1;
struct name2 n2;
…………………;

Then member of the outer structure are accessed by using structure name.member name but member of inner structure are accessed by using format:
Outer structure.inner structure.member name
i.e. member of the structure name1 are accessed by using format:
n2.n1.num1;

/*Program in which main structure is medicine (code, name, price), take a structure for date which can be used as inner structure and enter current and expiry date.  Check if the medicine is suitable for selling or not */

#include<stdio.h>
#include<conio.h>
struct date
{
       int m,y;
       };
struct medicines
{
       int cd,pr;
       char nm[100];
       struct date xd,cud;
       };
void main()
{
     struct medicines md;
     printf("\n enter medicine name");
     scanf("%s",md.nm);
     printf("\n enter medicine code and its price");
     scanf("%d%d",&md.cd,&md.pr);
     printf("\n enter current date of medicine month and year manufactured");
     scanf("%d%d",&md.cud.m,&md.cud.y);
     printf("\n enter expiry date of medicine month and year");
     scanf("%d%d",&md.xd.m,&md.xd.y);
     printf("\n medicine is %s \n code is %d \n price is %d",md.nm,md.cd,md.pr);
     printf("\n date of manufacturation is %d-%d",md.cud.m,md.cud.y);
     printf("\n expiry of medicine is %d-%d",md.xd.m,md.xd.y);
     if(md.cud.y<=md.xd.y)
     {
                          if(md.cud.m<md.xd.m)
                          printf("\n medincine can be sold");
                          else
                          printf("\n medicine can't be sold");
                          }
     getch();
     }
output of program to show whether the medicine is recommended for sale by checking its date using structures in C program


No comments:

Post a Comment