Nested Struct

Structure within a structure is called nested structure. There are two methods for declaring structure within a structure. Those are listed below. Normally the main structure is called the outer structure and the nested structure is called the inner structure.

Method 1

Declaring inner structure outside outer structure and declaring a variable of inner structure inside the outer structure. The syntax of the same is written below.

struct <inner structure name>{
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
};

struct <structure name>{
struct <inner structure name> <variable of innerstructure>;
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
};

Let us understand it by an example. In the below example, the outer structure is student and the inner structure is math_mark.
The student has 4 members: roll_no, name, percentage and structure math_mark itself. math_mark has a member n.

In this method, we have declared math_mark outside the structure of the student. And we are declaring the structure variable Vinner inside the structure student.

struct math_mark{
int n;
};

struct student{
struct math_mark Vinner;
int roll_no;
char name[20];
float percentage;
}stu1,stu2,stu3;

Method 2

Declaring inner structure inside the outer structure along with variables of inner structure inside the outer structure. The syntax of the same is written below.

struct <structure name>{
struct <inner structure name>{
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
}<variable of inner structure>;

<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
};

Same as method1 example, in the below example, the outer structure is student and the inner structure is math_mark. The student has 4 members: roll_no, name, percentage and structure math_mark itself. Math_mark has a member n.

Here we have declared math_mark and its variable Vinner inside the structure student.

struct <structure name>{
struct <inner structure name>{
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
}<variable of inner structure>;

<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
};

Accessing a member of the inner structure

The method to access a member of the inner structure is the same as accessing a member of a structure.

To access a member of a structure first we write the name of the structure variable and the name of the member which we want to use. And connect the name of the structure variable and the name of the member by full stop “.”. 

The same thing we do to access a member of an inner structure member. For this, we write outer structure variable name, inner structure variable name, member of inner structure and connect them with “.”.

The syntax of accessing a member of the inner structure is as below.

<outer structure variable name>.<inner structure variable name>.<member of inner structure>

 //of above example
stu1.Vinner.n

Example program for above both methods

Here the outer structure has 5 members: roll_no, name, percentage, structure math_mark, structure eng_mark.
The 1st inner structure math_mark has a member: n1. 2nd inner structure eng_mark has a member: n2.
stu1 is the variable of structure student, m1 is variable of structure math_mark, m2 is variable of eng_mark.

The below example is to get values of nested structures’ members and print them. Method 1 is used for math_mark and method 2 is used for eng_mark.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct math_mark{
    int n1;
};
struct student{
    struct math_mark m1;	//method 1
    struct eng_mark{
        int n2;
    }m2;		//method 2
    int roll_no;
    char name[20];
    float percentage;
}stu1;
void main()
{	clrscr();
    stu1.roll_no=24;
    strcpy(stu1.name,"Om");
    stu1.percentage=80;
    stu1.m1.n1=79;
    stu1.m2.n2=75;
    printf("roll_no=%d, \nname=%s, \npercentage=%f, \nmath_mark=%d , \neng_mark=%d",stu1.roll_no,stu1.name,stu1.percentage,stu1.m1.n1,stu1.m2.n2);
    getch();
}

Output:

Lesson learned from this blog:

1 ) Methods to use nested structures.
2) Syntax difference of both methods.
3 ) General names of both structures.
4 ) How to access members of nested structures.
5 ) How to make a program of nested structure.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments