Array of Structure and Structures with in a Structure

Introduction

Till now, the structure that we have defined can only contain one record. In practical terms, there may be a number of records that need to be entered and manipulated in a similar manner.

How do we solve the problem?

An impractical way to solve the problem would be to define a number of structured definitions for individual employees, like.

struct employee employee1 
struct employee employee2
and so on.

This kind of declaration would solve the problem, but it would create a problem when we want to search for the details of a particular employee or when the number of employees changes. The program would have to be modified to adjust all the details.

How do we solve this problem?

To solve the above problem, we make use of the array of structures.

An area of the structure is an array that is associated with the structure.

The following example would illustrate this concept.

Program for employee record maintenance using an array of structures.

#include <stdio.h>
#include <conio.h>

void main() {
    struct {
        int employee_code;
        char employee_name[30];
        char designation[20];
        double salary;
    } employee[2];

    int i;

    printf("Program by Shikha Tiwari to illustrate an array of structures\n");

    for (i = 0; i < 2; i++) {
        printf("\nEnter the details of employee %2d\n", i + 1);
        printf("Enter employee code: ");
        scanf("%d", &employee[i].employee_code);
        fflush(stdin);

        printf("Enter employee name: ");
        gets(employee[i].employee_name);
        fflush(stdin);

        printf("Enter employee designation: ");
        scanf("%s", employee[i].designation);
        fflush(stdin);

        printf("Enter employee salary: ");
        scanf("%lf", &employee[i].salary);
    }

    printf("\nYou have entered the following details for the employees:\n");

    for (i = 0; i < 2; i++) {
        printf("\nDetails for employee %2d\n", i + 1);
        printf("Employee code: %d\n", employee[i].employee_code);
        printf("Employee name: %s\n", employee[i].employee_name);
        printf("Designation: %s\n", employee[i].designation);
        printf("Salary: %.2lf\n", employee[i].salary);
    }
}

Explanation

In the above program, we have defined an array of structures. Each member of the structure is now associated with a subscript as an element of the array, and using the member operator and the relevant subscript, the different numbers of the array of structures are referred to and accessed.

Output

Output

Structure within a structure

'C' also permits the nesting of structures, which means that we can declare a structure within a structure. The syntax for defining a structure within a structure is similar to defining a structure itself.

Syntax for defining a structure within a structure.

struct[<structure tag>]
{
<type specifier><member1.1>;
struct<structure tag 2>
{
<type specifier><member2.1>;
.....;
}[<structure name 2>];
<type specifier><member1.2>;
.....;
[<structure name 1>]

For Example, a Program illustrates structure within a structure.

#include <stdio.h>
#include <conio.h>

void main() {
    struct date_of_joining {
        int day;
        int month;
        int year;
    };

    struct emp {
        int employee_code;
        char employee_name[30];
        char designation[20];
        double salary;
        struct date_of_joining d_o_j;
    } employee;

    clrscr();

    printf("Program to illustrate structure within a structure\n");

    printf("\nEnter the employee code: ");
    scanf("%d", &employee.employee_code);
    fflush(stdin);

    printf("\nEnter the employee name: ");
    gets(employee.employee_name);
    fflush(stdin);

    printf("\nEnter the employee designation: ");
    scanf("%s", employee.designation);
    fflush(stdin);

    printf("\nEnter the employee salary: ");
    scanf("%lf", &employee.salary);  // Changed to %lf for double

    printf("\nEnter the employee's date of joining:\n");

    printf("Enter the day of joining: ");
    scanf("%d", &employee.d_o_j.day);

    printf("Enter the month of joining: ");
    scanf("%d", &employee.d_o_j.month);

    printf("Enter the year of joining: ");
    scanf("%d", &employee.d_o_j.year);

    printf("\nYou have entered the following details for the employee:\n");
    printf("Employee code: %d\n", employee.employee_code);
    printf("Employee name: %s\n", employee.employee_name);
    printf("Designation: %s\n", employee.designation);
    printf("Salary: %.2lf\n", employee.salary);  // Changed to %.2lf for double
    printf("Joining date: %02d-%02d-%04d\n", employee.d_o_j.day, employee.d_o_j.month, employee.d_o_j.year);

    getch();
}

Output

Program

Explanation

In the above Program, we have defined the structure within a structure.

The declaration of the structure that is to be nested (date_of_joining) within another structure(emp) is made before the structure emp. This is done because the structured data_of_joining is the member of the structure emp, and until and unless the structure that is to be made a member of another structure is defined, we cannot use the structure. Therefore, we have defined the structured date_of_joining before the structure employee and have declared a structure variable inside the structure employee, date_of_joining, to store the related information. To access the members of the structure within the structure, two member operators '.' are used as shown in the above example. The first dot indicates the parent structure employee, and the second dot indicates the child structured d_o_j. The number of dots is directly proportional to the number of structures nested.

Summary

In C, an array of structures allows multiple instances of a structure to be stored in a single array, where each element of the array is a structure of the same type. This is useful for grouping related data together, such as student records or employee details, with each structure representing an individual entity. Structure within a structure (also called nested structures) involves including one structure as a member of another structure.

This allows complex data hierarchies to be modeled, such as storing address details within an employee structure.


Similar Articles