C Program to Implement an array of the Nested Structures and accessing them using a single pointer and array of pointers..

#include<stdio.h>

struct address
{
 char city[20],state[20],country[20];
};

struct student
{
 char name[20];
 int rollno;
 int mark;
 struct address add;
};

void sort(struct student *p,int n)
{
 struct student t;
 int i,j;
 for(i=0;i<n-1;i++)
 {
  for(j=0;j<n-1-i;j++)
   {
       if(p[j].rollno>p[j+1].rollno)
      {
       t=p[j];
       p[j]=p[j+1];
       p[j+1]=t;
      }
   }
 }
}


void main()
{
 struct student s[100],*ptrs,*ptr[100];
 int n,i;
 printf("Enter the no of students..");
 scanf("%d",&n);
 printf("\nEnter the student details one by one..");
 for(i=0;i<n;i++)
 {
  printf("\nStudent %d details : ",i+1);
  printf("\nEnter the name..");
  scanf("%s",s[i].name);
  printf("\nEnter the rollno..");
  scanf("%d",&s[i].rollno);
  printf("\nEnter the mark..");
  scanf("%d",&s[i].mark);
  printf("\nEnter the city..");
  scanf("%s",s[i].add.city);
  printf("\nEnter the state..");
  scanf("%s",s[i].add.state);
  printf("\nEnter the country..");
  scanf("%s",s[i].add.country);
  ptr[i]=&s[i];
}
 sort(s,n);
 printf("\nThe student details as per sorted order using single pointer..");
 ptrs=s;
 for(i=0;i<n;i++)
 {
  printf("\nStudent %d details : ",i+1);
  printf("\nThe rollno is ..%d",ptrs->rollno);
  printf("\nThe name is..%s",ptrs->name);
  printf("\nThe mark is..%d",ptrs->mark);
  printf("\nThe Address is..%s,%s,%s.",ptrs->add.city,ptrs->add.state,
  ptrs->add.country);
  ptrs++;
  printf("\n");
 }
 printf("\nThe student details as per sorted order using array of pointers..");
 for(i=0;i<n;i++)
 {
  printf("\nStudent %d details : ",i+1);
  printf("\nThe rollno is ..%d",ptr[i]->rollno);
  printf("\nThe name is..%s",ptr[i]->name);
  printf("\nThe mark is..%d",ptr[i]->mark);
  printf("\nThe Address is..%s,%s,%s.",ptr[i]->add.city,ptr[i]->add.state,
  ptr[i]->add.country);
  printf("\n");
 }
}

No comments:

Post a comment