Nested Structure in C

Nested Structures in C. Nested Structures in C Tutorial. Learn Nested Structures in C
Nested Structure in C.

What is Nested Structure in C ?

In C programming, a nested structure refers to a structure that is defined within another structure. It allows you to create a hierarchy or grouping of related data by combining multiple structures together.

Nested structures are used when you need to represent complex relationships between data elements, where one structure contains another structure as a member. This can help in organizing and managing data more efficiently, providing a logical and intuitive structure to represent real-world entities.

Syntax:

struct OuterStructure {
    // Outer structure members
    // ...
    
    struct InnerStructure {
        // Inner structure members
        // ...
    } inner;  // Inner structure variable declaration
};

Here, an inner structure is defined within the outer structure, and you can declare an instance of the inner structure as a member of the outer structure.

Examples of Nested Structure in C

Examples 1: Department and Student Information

#include <stdio.h>

struct Student {
    char name[20];
    int age;
};

struct Department {
    char deptName[30];
    struct Student student;
};

int main() {
    struct Department department = { "Computer Science", { "John Doe", 20 } };

    printf("Department: %s\n", department.deptName);
    printf("Student Name: %s\n", department.student.name);
    printf("Student Age: %d\n", department.student.age);

    return 0;
}

Output:

Department: Computer Science
Student Name: John Doe
Student Age: 20

Explanation:

In above example, we have a nested structure Student inside the Department structure. The Department structure contains the name of the department and an instance of the Student structure. We initialize and access the nested structure members in the main() function.

Example 2: Employee with Address Details

#include <stdio.h>

struct Address {
    char street[50];
    char city[30];
    char state[20];
};

struct Employee {
    char name[50];
    int age;
    struct Address address;
};

int main() {
    struct Employee employee = { "Jane Smith", 28, { "123 Main St", "New York", "NY" } };

    printf("Employee Name: %s\n", employee.name);
    printf("Employee Age: %d\n", employee.age);
    printf("Employee Address: %s, %s, %s\n", employee.address.street, employee.address.city, employee.address.state);

    return 0;
}

Output:

Employee Name: Jane Smith
Employee Age: 28
Employee Address: 123 Main St, New York, NY

Explanation:

The above example demonstrates a more complex nested structure. The Employee structure contains the name, age, and an instance of the Address structure. The Address structure represents the address of an employee. We initialize and access the nested structure members in the main() function.

Example 3: University, Students, and Courses Data

#include <stdio.h>

struct Course {
    char courseName[50];
    int credits;
};

struct Student {
    char name[50];
    int age;
    struct Course courses[5];
};

struct University {
    char name[100];
    struct Student students[1000];
};

int main() {
    struct University university = {
        "ABC University",
        {
            { "John Doe", 20, { { "Math", 4 }, { "Physics", 3 }, { "Computer Science", 5 } } },
            { "Jane Smith", 21, { { "English", 3 }, { "Chemistry", 4 } } }
            // ... additional students
        }
    };

    printf("University Name: %s\n", university.name);
    printf("Student Name: %s\n", university.students[0].name);
    printf("Course Name: %s\n", university.students[0].courses[0].courseName);
    printf("Course Credits: %d\n", university.students[0].courses[0].credits);

    return 0;
}

Output:

University Name: ABC University
Student Name: John Doe
Course Name: Math
Course Credits: 4

Explanation:

In above example, the University structure contains the name of the university and an array of Student structures. Each Student structure contains the name, age, and an array of Course structures representing the courses the student is enrolled in. We initialize and access the nested structure members in the main() function.

More: