In this article Array of Structures in C we give the information about Structure can be used in combination with Array. We know that Structure is used to access groups of related data items.

Array of Structures in C:

Array of Structures: Structure can be used in combination with Array. We know that Structure is used to access groups of related data items. We declare variables to access different members of the structure. When we have to declare many variables in a program, we use Array to avoid this problem.

Syntax for Array Structure Declaration

 struct structure_name structure_variable_name[array_size];

Example for Array Structure Declaration

 struct Employee info[3];

For example, if the names of all the students of a class and the marks obtained by that student in various subjects are to be stored, then using Array proves useful. As:

struct marks

{

    int sub1;

    int sub2;

    int sub3;

};

main()

{

    static struct marks Student[4] = {{41,12,22}, {52,55,66}, {32,55,68}, {55,44,88}};

}

If we have to store the marks obtained by four students in three subjects, then it is better to declare the variable as Array type.

Example for Structure without using Array

Source Code:

#include <stdio.h>

#include <string.h>

struct Employee

{

    int emp_id;

    char emp_name[20];

    float salary;

};

int main()

{

    struct Employee info1, info2, info3;

    info1.emp_id=34;

    strcpy(info1.emp_name, “Raj”);

    info1.salary = 20000.00;

    info2.emp_id=35;

    strcpy(info2.emp_name, “Maruti”);

    info2.salary = 30000.00;

    info3.emp_id=36;

    strcpy(info3.emp_name, “Suraj”);

    info3.salary = 40000.00;

    printf(“Student 1\n”);

    printf(“Employee id is : %d\n”, info1.emp_id);

    printf(“Employee name is %s\n”, info1.emp_name);

    printf(“Employee salary is : %f\n\n”, info1.salary);

    printf(“Student 2\n”);

    printf(“Employee id is : %d\n”, info2.emp_id);

    printf(“Employee name is %s\n”, info2.emp_name);

    printf(“Employee salary is : %f\n\n”, info2.salary);

    printf(“Student 3\n”);

    printf(“Employee id is : %d\n”, info3.emp_id);

    printf(“Employee name is %s\n”, info3.emp_name);

    printf(“Employee salary is : %f\n\n”, info3.salary);

return 0;

}

Output :

Student 1

Employee id is : 34

Employee name is Raj

Employee salary is : 20000.000000

Student 2

Employee id is : 35

Employee name is Maruti

Employee salary is : 30000.000000

Student 3

Employee id is : 36

Employee name is Suraj

Employee salary is : 40000.000000

Example for Structure using Array

#include <stdio.h>

#include <string.h>

struct Employee

{

    int emp_id;

    char emp_name[20];

    float salary;

};

int main()

{

    struct Employee info[3];

    int i;

    info[0].emp_id=34;

    strcpy(info[0].emp_name, “Raj”);

    info[0].salary = 20000.00;

    info[1].emp_id=35;

    strcpy(info[1].emp_name, “Maruti”);

    info[1].salary = 30000.00;

    info[2].emp_id=36;

    strcpy(info[2].emp_name, “Suraj”);

    info[2].salary = 40000.00;

    for(i=0; i<3; i++)

   {

    printf(“Student %d\n”, i+1);

    printf(“Employee id is : %d\n”, info[i].emp_id);

    printf(“Employee name is %s\n”, info[i].emp_name);

    printf(“Employee salary is : %f\n\n”, info[i].salary);

}

return 0;

}

Output :

Student 1

Employee id is : 34

Employee name is Raj

Employee salary is : 20000.000000

Student 2

Employee id is : 35

Employee name is Maruti

Employee salary is : 30000.000000

Student 3

Employee id is : 36

Employee name is Suraj

Employee salary is : 40000.000000

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *