Array Of Structure In C

Array Of Structure In C

Array Of Structure In C

Array Of Structure In C


Array Of Structure In C


Structure and its types in C


     1. General structure
     2. Structure with in structure

     3. Array of structure


General structure
Program :

#include
#include
struct arun
{
char name[20];
int age;
int mark;
};
void main()
{
struct arun a;
{
clrscr();
printf("Enter the name is:");
scanf("%s",&a.name);
printf("Enter the age is:");
scanf("%d",&a.age);
printf("Enter the mark 1 is:");
scanf("%d",&a.mark1);
printf("\n The given name is %s",a.name);
printf("\n The given age is %d",a.age);
printf("\n The given mark1 is %d",a.mark1);
}
getch();
}

Structure with structure
Program :

#include
#include
struct total
{
char name[20];
int age;
};
struct san
{
int mark1;
int mark2;
int total;
int avg;
struct total t;
};
void main()
{
struct san s;
{
clrscr();
printf("Enter the name is :");
scanf("%s",&s.t.name);
printf("Enter the age is:");
scanf("%s",&s.t.age);
printf("Enter the mark1 is:");
scanf("%s",&s.mark1);
printf("Enter the mark2 is :");
scanf("%s",&s.mark2);
s.total=s.mark1+s.mark2;
s.avg=s.total/2;
printf("\n The given name is %s",s.t.name);
printf("\n The given age is %d",s.t.age);
printf("\n The given mark1 is %d",s.mark1);
printf("\n The given mark2 is %d",s.mark2);
printf("\n The total is %d",s.total);
printf("\n The average is %d",s.avg);
}
getch();
}

Array of structure
Program :


#include
#include
struct total
{
char name[20];
int age;
int mark1;
int mark2;
int total;
int avg;
};
void main()
{
int i;
struct total s[3];
clrscr();
for(i=1;i<=3;i++)
{
clrscr();
printf("Enter the name is :");
scanf("%s",&s[i].name);
printf("Enter the age is:");
scanf("%s",&s[i].age);
printf("Enter the mark1 is:");
scanf("%d",&s[i].mark1);
printf("Enter the mark2 is:");
scanf("%d",&s[i].mark2);
s[i].total=s[i].mark1+s[i].mark2;
s[i]avg=s[i].total/2;
}
for(i=1;i<=3;i++)
{
printf("\n The given name is%s",s[i].name);
printf("\n The given age is%d",s[i].age);
printf("\n The given mark1 is%d",s[i].mark1);
printf("\n The given mark2 is%d",s[i].mark2);
printf("\n The total is %d",s[i].total);
printf("\n The average is %d",s[i].avg);
}
getch();
}

Post a Comment

0 Comments