Home >>C Tutorial >C Nested Structure

Nested Structure in C

Nested structures in C

Nested structure in C is one of the exciting feature that is provided by the C language. Using this feature users can nest one structure within any other structure and using this the creation of complex data types happens. For instance, in a structure, the address of an entity employee is required to be stored. And the subparts that are to be included in the attribute address like, address, pincode etc. In order to store the address of an employee, the users are required to address of the entity that is employee in to a separate structure and then there is a need to nest the structure address in to the structure employee.

Example will display here

There are generally two ways that are known to nest one structure in to another.

  • Separate Structure
  • Embedded structure

1. Separate structure in C

In separate structure, two structures are created but the catch is that the dependent structure must be used as a member inside the main structure.

Example will display here

2. Embedded structure in C

In order to declare a structure inside a structure, embedded structure in C is used. Because of this feature the lines of code required are less in amount but there is a limitation that it cannot be used in multiple data structures.

Example will display here

Let's understand accessing the nested structure in C

Outer_Structure.Nested_Structure.member is used in order to access the member of the nested structure in C.

Here is an example that will explain these things:

  • e1.doj.dd
  • e1.doj.mm
  • e1.doj.yyyy
Example will display here

Let's understand the procedure of passing structure to function in C

As a fact, we know that similar to variables, the structures can also be passed to a function. Either the user can pass the structure members in to the function or they can just pass the structure variable at once.

Example will display here

No Sidebar ads