Nested Structure in C
- 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.
- Structure within structure in C using normal variable
- 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.
- struct student_college_detail
- // structure within structure
- struct student_college_detail clg_data;
- struct student_detail stu_data = {1, "Raju", 90.5, 71145,
- printf(" Id is: %d \n", stu_data.id);
- printf(" Name is: %s \n", stu_data.name);
- printf(" Percentage is: %f \n\n", stu_data.percentage);
- printf(" College Id is: %d \n",
- stu_data.clg_data.college_id);
- printf(" College Name is: %s \n",
- stu_data.clg_data.college_name);
Output:
Id is: 1
Name is: Raju
Percentage is: 90.500000
College Id is: 71145