Monday, February 7, 2011

Structure within Structure in C programming


We have done programs on C structure where structure member are normally primitive data members. Now we will see how to use structure variables as a member of another structure.

#include< stdio.h>
struct cal
{
 int feet;
 float inch;

 };
 struct Room
 {
 struct cal length;
 struct cal width;
 };
 void main()
 {
 struct Room ob;
 float l,w;
    clrscr();
 ob.length.feet=10;
 ob.length.inch=2.5;
 ob.width.feet=10;
 ob.width.inch=2.5;
 l=ob.length.feet+ob.length.inch/12;
 w=ob.width.feet+ob.width.inch/12;
 l=l*w;
 printf("Area of the Room %.2f",l);
  getch();
  }

Another program on structure within structure.

# include< stdio.h>
struct Addr
{
            char street[30];
            char city [30];
            char state[20];
            long  pin;
};
struct Customer
{
            char name[20];
            char account[10];
            char profession[30];
            struct Addr address;
};
void main()
{
   struct Customer *list[100];
   int i, n;
   clrscr();
   printf("\n Input number of record you want to process: ");
   scanf("%d", &n);
   printf("\n Input the information of customer\n");
   for(i=0; i< n; i++)
   {
        fflush(stdin);
       printf("\n Name: ");
       gets(list[i]->name);
       printf("\n Account Number:");
       gets(list[i]->account);
       printf("\n Profession:");
       gets(list[i]->profession);
       printf("\n*** Address***");
       printf("\n**************\n");
       printf("\n Street:");
         gets(list[i]->address. street);
         printf("\n City:");
         gets(list[i]->address. city);
         printf("\n State:");
         gets(list[i]->address. state);
         printf("\n Pin Code:");
         scanf("%ld", &list[i]->address. pin);
         clrscr();
     }

   for(i= 0; i< n; i++)
   {
     printf("\n List of the customer");
     printf("\n ********************\n");
     printf("\n Name: %s", list[i]->name);
     printf("\n Account Number: %s", list[i]->account);
     printf("\n Profession: %s", list[i]->profession);
     printf("\n *** Address ***");
     printf("\n----------------");
     printf("\n Street: %s", list[i]->address. street);
      printf("\n City: %s", list[i]->address. city);
      printf("\n State: %s", list[i]->address. state);
    printf("\n Pin code: %ld", list[i]->address. pin);
    printf("\n*******************");
    printf("\nPress any key...........");
    getch();
     clrscr();
   }
 }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner