OverIQ.com

Nested Structures in C

Last updated on July 27, 2020


A structure can be nested inside another structure. In other words, the members of a structure can be of any other type including structure. Here is the syntax to create nested structures.

Syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
structure tagname_1
{
    member1;
    member2;
    member3;
    ...
    membern;

    structure tagname_2
    {
        member_1;
        member_2;
        member_3;
        ...
        member_n;
    }, var1

} var2;

Note: Nesting of structures can be extended to any level.

To access the members of the inner structure, we write a variable name of the outer structure, followed by a dot(.) operator, followed by the variable of the inner structure, followed by a dot(.) operator, which is then followed by the name of the member we want to access.

var2.var1.member_1 - refers to the member_1 of structure tagname_2
var2.var1.member_2 - refers to the member_2 of structure tagname_2
and so on.

Let's take an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
struct student
{
    struct person
    {
        char name[20];
        int age;
        char dob[10];
    } p ;

    int rollno;
    float marks;
} stu;

Here we have defined structure person as a member of structure student. Here is how we can access the members of person structure.

stu.p.name - refers to the name of the person
stu.p.age - refers to the age of the person
stu.p.dob - refers to the date of birth of the person

It is important to note that structure person doesn't exist on its own. We can't declare structure variable of type struct person anywhere else in the program.

Instead of defining the structure inside another structure. We could have defined it outside and then declare it's variable inside the structure where we want to use it. For example:

1
2
3
4
5
6
struct person
{
    char name[20];
    int age;
    char dob[10];
};

We can use this structure as a part of a bigger structure.

1
2
3
4
5
6
struct student
{
    struct person info;
    int rollno;
    float marks;
}

Here the first member is of type struct person. If we use this method of creating nested structures then you must first define the structures before creating variables of its types. So, it's mandatory for you to first define person structure before using it's variable as a member of the structure student.

The advantage of using this method is that now we can declare a variable of type struct person in anywhere else in the program.

Nesting of structure within itself is now allowed. For example:

1
2
3
4
5
6
7
8
struct citizen
{
    char name[50];
    char address[100];
    int age;
    int ssn;
    struct citizen relative; // invalid
}

Initializing nested Structures #

Nested structures can be initialized at the time of declaration. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
struct person
{
    char name[20];
    int age;
    char dob[10];
};

struct student
{
    struct person info;
    int rollno;
    float marks[10];
}

struct student student_1 = {
                               {"Adam", 25, 1990},
                               101,
                               90
                           };

The following program demonstrates how we can use nested structures.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>

struct person
{
    char name[20];
    int age;
    char dob[10];
};

struct student
{
    struct person info;
    int roll_no;
    float marks;
};

int main()
{
    struct student s1;

    printf("Details of student: \n\n");

    printf("Enter name: ");
    scanf("%s", s1.info.name);

    printf("Enter age: ");
    scanf("%d", &s1.info.age);

    printf("Enter dob: ");
    scanf("%s", s1.info.dob);

    printf("Enter roll no: ");
    scanf("%d", &s1.roll_no);

    printf("Enter marks: ");
    scanf("%f", &s1.marks);

    printf("\n*******************************\n\n");

    printf("Name: %s\n", s1.info.name);
    printf("Age: %d\n", s1.info.age);
    printf("DOB: %s\n", s1.info.dob);
    printf("Roll no: %d\n", s1.roll_no);
    printf("Marks: %.2f\n", s1.marks);

    // signal to operating system program ran fine
    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Details of student:

Enter name: Phil
Enter age: 27
Enter dob: 23/4/1990
Enter roll no: 78123
Enter marks: 92

*******************************

Name: Phil
Age: 27
DOB: 23/4/1990
Roll no: 78123
Marks: 92.00

How it works:

In lines 3-8, we have declared a structure called person.

In lines 10-15, we have declared another structure called student whose one of the members is of type struct student (declare above).

In line 19, we have declared a variable s1 of type struct student.

The next five scanf() statements (lines 23-36) asks the user to enter the details of the students which are then printed using the printf() (lines 40-44) statement.