Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

C – Array of Structure

Same as an array of character, integer, float, etc. we can create an array of structure as well.

Example

#include<stdio.h>
#include<string.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    int i;
    struct Employee emp[3];  // Array of structure Employee
    for(i=0;i<3;i++)
    {
        printf("\n Enter ID NO. of employee %d = ",i+1);
        scanf("%d",&emp[i].id_no);
        printf("\n Enter NAME of employee %d = ",i+1);
        scanf("%s",emp[i].name);
        printf("\n Enter SALARY of employee %d = ",i+1);
        scanf("%f",&emp[i].salary);
    }
	
    for(i=0;i<3;i++)
    {
        printf("\n\n");
        printf("\n ID NO. of employee %d = %d",i+1,emp[i].id_no);
        printf("\n NAME of employee %d = %s",i+1,emp[i].name);
        printf("\n SALARY of employee %d = %f",i+1,emp[i].salary);
    }

    return 0;
}

Output

 
 Enter ID NO. of employee 1 = 1

 Enter NAME of employee 1 = JOHN

 Enter SALARY of employee 1 = 55000.69

 Enter ID NO. of employee 2 = 2

 Enter NAME of employee 2 = JAMES

 Enter SALARY of employee 2 = 45987.90

 Enter ID NO. of employee 3 = 3

 Enter NAME of employee 3 = JOHNSON

 Enter SALARY of employee 3 = 56985.09

 ID NO. of employee 1 = 1
 NAME of employee 1 = JOHN
 SALARY of employee 1 = 55000.691406

 ID NO. of employee 2 = 2
 NAME of employee 2 = JAMES
 SALARY of employee 2 = 45987.898438

 ID NO. of employee 3 = 3
 NAME of employee 3 = JOHNSON
 SALARY of employee 3 = 56985.089844

 

Leave a reply