C Programming language

adplus-dvertising
C - Example of Array of structure
Previous Home Next
#include <stdio.h>
#include<conio.h>
void main()
{
int i=0;
 /* defining a structure for employee record */
struct employeeinfo         
     {
      int emp_id;
      char name[21];
      char sex;
      int salary;
   };
  /*initializing the record values in an array of structure of type employeeinfo */
employeeinfo data[3] = {{1001,
"Ashish", 'M', 10000}, {1002,"Swati", 'F', 8000}, {1003,"Praveen", 'M', 15000}}; 
clrscr();
printf("\nThe content of record of employee are as follows :");  
/* displaying the record of the employees */
for (i=0;i<=2;i++)
    {
     printf("\n Record number %d", i+1 );
     printf("\n Employee id is : %d", data[i].emp_id);   
/* accessing the records of array data */
     printf("\n Employee name is : %s", data[i].name);
     printf("\t Employee gender is : %c", data[i].sex);
     printf("\t Employee salary is : %d", data[i].salary);
   }
 getch();
}        

The above program shows how to an array of structure which can hold record of 3 employees simultaneously and how we can access those records via using the concept of array of structures.

Output : Following would be the desired output of the above  written program

The content of record of employee are as follows :

Record number 1

Employee id is 1001    Employee name is Ashish    Employee gender is M     Employee salary is 10000

Record number 2

Employee id is 1002   Employee name is Swati     Employee gender is F     Employee salary is 8000

Record number 3

Employee id is 1003    Employee name is Praveen   Employee gender is M     Employee salary is 15000

Previous Home Next