Nested Structure in C with Examples

This C Tutorial explains Nested structure in C with examples and How can we access them.

What is Nested Structure in C?
Nested structure is a structure whose member is itself a structure.

Let’s see the Nested structure declaration below,

struct A {
          int a;
          float b;
    };
 
struct B {
          int c;
          float d;
          struct A e;
    };

So, what have you noticed here? The member ‘e’ of struct B is itself a structure of type struct A. While being compiled by the compiler, line,

    struct A e;

doesn’t put any error. Actually, member ‘e’ of structure B is itself a structure, type struct A, which is already declared before structure type B. Therefore, compiler doesn’t have any problem in evaluating space allocation to member ‘e’ of structure B.

advertisement
advertisement

So, we can say that a structure whose member is itself a structure is called a nested structure. Let’s learn to access a nested structure in C,

The structure can be nested in the following ways. they are

  • Separate Nested Structure
  • Embedded Nested Structure

Separate Nested Structure
In this approach, two structures are created, but the dependent structure must be implemented as a member of the main structure.

Syntax:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
struct A {
          int a;
          float b;
    };
 
struct B {
          int c;
          float d;
          struct A e;
    };

Embedded Nested Structure
This approach uses fewer lines of code and allows you to declare a structure inside another structure.

Syntax:

struct B
{
    int c;
    float d;
    struct A
    {
        int a;
        float b;
    }e;
};

Nested Structure Example:

advertisement
#include <stdio.h>
 
/* structure A declared */
typedef struct A {
          int a;
          float b;
    }New_a;
 
/* structure B declared */
typedef struct B {
          int c;
          float d;
          struct A e;    /* member 'e' is itself a structure */
    }New_b;
 
int main(void)
{
    /* Let's declare variables of New_a and New_b */
    New_a bread;
    New_b butter;        /* 'butter' is a nested structure */
 
    /* Let's access bread using dot operator */
    bread.a = 10;        /* assigned member a value 10 */
    bread.b = 25.50;
 
    /* Let's access butter using dot operator */
    butter.c = 10;
    butter.d = 50.00;
 
    /* Let's access member 'e' which is a nested structure */
    butter.e.a = 20;
    butter.e.b = 20.00;
 
    /* Display values of members of 'butter.e' structure */
    printf("butter.e.a is %4d\n", butter.e.a);
    printf("butter.e.b is %.2f\n", butter.e.b);
 
    return 0;
}

Output of the above program follows,

butter.e.a is   20
butter.e.b is 20.00

Notice that ‘e’ is a member of structure butter and at the same time it’s a structure of type struct A. Therefore, we used dot operator to access ‘e’ in butter, as,

    butter.e

but ‘butter.e’ is a structure of type struct A. struct A has two members viz. an integer and a float. To access these two members of

advertisement
    butter.e

we again used dot operator as,

    butter.e.a = 20;

and assigned an integer value 20 to member ‘a’ of butter.e. Similarly, we accessed member ‘b’ of

   butter.e
   butter.e.b = 20.00;

and assigned it with float value.

Not just this way you can access nested structures. What if we don’t know the structure by name and we have a pointer to structure instead, for example,

    New_b *p2b = &butter;

Then, how will you access members of ‘butter’ structure? We can use pointer with indirection as well as arrow operators to access them. Let’s understand difference between using pointer with indirection and arrow operator before we try them out to access members of butter. Notice that exp.

    p2b;

is a pointer to ‘butter’ New_b structure. Applying indirection on this, i.e.

    *p2b;

results in value where p2b is pointing to. As an R-Value, this is the value of entire structure ‘butter’ which can be thought of as equivalent to structure name ‘butter’. And as an L-Value, this refers to the entire location occupied by ‘butter’; wherein ‘butter’ members can receive new values. Let’s consider the R-Value to access the members of ‘butter’,

    (*p2b).member_of_butter;

Notice the parenthesis used around ‘*p2b’. Since dot ‘.’ operator has higher precedence than ‘*’ operator. Parenthesis cause ‘*p2b’ to go first. Then ‘.’ operator selects member of ‘butter’. For example,

    (*p2b).c;            /* integer member of 'butter' */

Now, we try using arrow ‘->’ operator to access ‘butter’ and its members. Arrow operator has indirection built into it. Therefore, exp.

    p2b->c;

means indirection performed on ‘p2b’ and selected member ‘c’ of ‘butter’. We prefer arrow operator because of its convenience. Let’s access members of ‘butter’ using pointer-to-butter rather than by name of ‘butter’,

    printf("p2b->c is an integer %d\n", p2b->c);
    printf("p2b->d is a float %f\n", p2b->d);
 
    /* 'e' member is a nested structure, let's access this too */
    printf("p2b->e.a is an integer %d\n", p2b->e.a);
    printf("p2b->e.b is a float %f\n", p2b->e.b);

Let’s understand the expression,

    p2b->e.a;

p2b->e selects member ‘e’ of ‘butter’, but ‘e’ is a structure of type struct A. Therefore, exp.

    p2b->e

represents a strucute of type struct A. Structure A has two members, an integer and a float. To access their values, we used dot operator, like,

    p2b->e.a;

selects integer member of ‘p2b->e’ structure. Likewise, we accessed float member,

    p2b->e.b;

Though ‘.’ operator has higher precedence than ‘->’ operator, but their associativity is from left to right. Hence, in exp.

    p2b->e.a;

‘p2b->e’ is evaluated first then dot operator gets evaluated.

Passing Nested Structure to Function
When passing a nested structure to a function, you must first declare the structure type with the keyword struct. The following example shows how to declare a nested structure type:

struct Student
{
    char name[50];
    int age;
    struct Address
    {
        char street[50];
        char city[50];
        char state[50];
        int zip;
    } address;
};

The next step is to create a variable of the nested structure type. This is done by using the keyword struct followed by the name of the structure, as shown below:

struct Student student1;

Once you have declared a variable of the nested structure type, you can access the individual members of the nested structure by using the dot operator (.), as shown in the following example:

student1.address.street = “123 Main Street”;

To access the members of the nested structure, you must first dereference the pointer to the outermost structure. Then, you can use the arrow operator (->) to access the members of the innermost structure.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.