Explain Nested structure in C with Examples

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

Let’s see the 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,

advertisement
    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.

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, for example,

#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,

advertisement
    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

    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.

advertisement

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.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn