x

C Nested Structure

Prev     Next

  • Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure.
  • The structure variables can be a normal structure variable or a pointer variable to access the data. You can learn below concepts in this section.
  1. Structure within structure in C using normal variable
  2. Structure within structure in C using pointer variable

1. Structure within structure in C using normal variable:

  • This program explains how to use structure within structure in C using normal variable. “student_college_detail’ structure is declared inside “student_detail” structure in this program. Both structure variables are normal structure variables.
  • Please note that members of “student_college_detail” structure are accessed by 2 dot(.) operator and members of “student_detail” structure are accessed by single dot(.) operator.

Output:

Id is: 1
Name is: Raju
Percentage is: 90.500000
College Id is: 71145
College Name is: Anna University

Structure within structure (nested structure in C ) using pointer variable:

  • This program explains how to use structure within structure in C using pointer variable. “student_college_detail’ structure is declared inside “student_detail” structure in this program. one normal structure variable and one pointer structure variable is used in this program.
  • Please note that combination of .(dot) and ->(arrow) operators are used to access the structure member which is declared inside the structure.

Output:

Id is: 1
Name is: Raju
Percentage is: 90.500000

College Id is: 71145
College Name is: Anna University



Continue on C – Structure Memory Allocation….

Continue on C – Structure Padding….

Prev     Next



Like it? Please Spread the word!