Tuesday, 21 April 2015

structures with arrays in C

The following simple code demonstrates use of structures with arrays in C

#include stdio.h
#include conio.h
#include string.h
#define max_no 20

/*defining structure */
typedef struct{
char name[15];
int age;
}person;

void main(){
person p[max_no]; // defining structure with array
int i,j,max;

printf("enter the max number of students\n");
scanf("%d",&max);

for( i=0;i<max;i++)
{
printf("\n enter name : ");
scanf("%s",&p[i].name);
printf("\n enter age for %s : ",p[i].name);
scanf("%d",&p[i].age);
}//end of for loop

printf("\n Name \t age \n");
printf("-----------\n");

for(j=0;j<max;j++)
{
printf(" %s \t %d \n",p[j].name,p[j].age);
printf("-----------\n");
}//end of for loop

getch(); // used to hold the display
}//end of main



Output

structures with arrays

No comments:

Post a Comment