Nested structures in c

Nested structures in c

Nested Structure In C programming, when we declare another sub-structure in a structure. Those which are declared as another structure member inside a structure. They are called nested structure data type. Whenever you define new other types of structure members as data type inside another structure. Nested structures help you to create complex data structures in C programs. Where each member of the declared nested structure can itself be a structure group data type element.

Nested structures in c

So let’s declare a nested structure in C programming.

Example of Nested Structure in C.

So let’s create an example of a nested structure program in C program. In which nested structure data type variables are present, which displays the basic information of an employee. In which all the details of the employee and other variable element information are displayed.

#include <stdio.h>

#include <string.h>

// Define an employee structure for id

struct id {

char dep[50]

int id;

int contact; // Define a structure for employee

struct employee {

char name[50];

int age; struct-ididentification; // Nested structure element declare here

float salary;

};

int main() {

// let Declare a variable of type struct employee

struct employee employee0;

// Assign values ​​​​to structure members

strcpy(employee.name, “Jim Karlos”);

employee0.age = 35; employee0.identification.id = 101;

employee0.identification.contact = 9413;

employee0.salary = 600.95 ;

// let Access and print structure members in nested structure

printf(“\n —-employee information —-“);

printf(“\n employee Name – %s”, employee0.name);

printf(“\n employee Age – %d”, employee0.age);

printf(“\n employee department – %s/%d/%d/%d”, employee0.identification.name, employee0.identification.age,employee0.identification.contact);

printf(“\n employee salary – %.f”, employee0.salary);

return 0;

}

Nested Structure Program Explanation.

id structure declaration.

Three different structure members are declared in identification structure, and id structure is defined by the name identification. In which you get structure members like id, dep, contact, etc. in nested structure data type.

Employee structure declaration.

Employee structure data types are employee name (character array), employee age (integer), salary (float) data types (a floating-point number) data variables.

Here id identification is a nested structure data type. It displays the department information in the id identification in a nested structure.

The main function in the structure.

In the structure main() a variable employee0 of structure employee type is declared.

Where default storage values ​​of employee structure members are assigned values ​​using the dot (.) operator for direct members and nested dot (.) operator for default members of nested structure (id) identification.

Printing a structure variable in a C program.

Prints the employee information stored in employee0 structure. In which all the variable elements of the Employee structure have been printed.

Advantages of Nested Structures in C Programs.

Structure Organization – In nested structure, you design the structure in a sequence from top to bottom. In which parent structure and child structure are member elements. Nested structure makes the large program source code more readable and intuitive.

Structure Encapsulation – In nested structure, parent and child structures group the related data together. Due to which nested structure is easy in program code maintenance and there is no error in structure member data association.

Structure Modularity – In nested structure, each structure is defined as a separate structure and can be divided into sub-modules in different parts of the program compilation and can be reused anytime. Due to which nested structure is called modular programming.

Nested Structure Define.

Before declaring any nested structure, declare the structure as I and initialize and assign appropriate structure data values ​​to the outer and nested structure members.

Access nested structure members using the dot (.) operator for each level of data nesting in a nested structure.

Nested structures in C are particularly useful when defining complex data types or dealing with multiple structure relationships or creating and representing submodular structures.