Mutually Dependent Structures in C

Question: What are Mutually Dependent Structures in C?

Answer: Two structures, for example, say A and B, are called mutually dependent if one of members of each refers to other structure. For example,

struct A {
            struct B b;
            int value;
    };
 
struct B {
           struct A a;
           float wool;
    };

Can these structure declarations suffice condition for mutually dependent structures?
Surely, member ‘b’ of struct A refers to struct B while member ‘a’ of struct B refers to struct A. But what happens when you compile the above fragment of code. Compiler gives error as it processes line

    struct B b;

and the error is struct B is not known before this line. So, we try to reverse the declarations as,

struct B {
           struct A a;
           float wool;
    };
struct A {
            struct B b;
            int value;
    };

Now, we put struct B declaration before struct A declaration and compiled the fragment of code. Again, same type of error and this is struct A is not known while compiling the line

advertisement
advertisement
    struct A a;

Should we have used pointer as a member of first struct to refer to second structure
and vice-versa? Let us try this also,

struct B {
           struct A *a;
           float wool;
    };
 
struct A {
            struct B *b;
            int value;
    };

What about now? There’s no problem to compiler in deciding how much memory to allocate
to member ‘a’ of struct B i.e. in lines

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
struct B {
            struct A *a;

because ‘a’ is a pointer. But ‘a’ is a pointer to struct A and struct A isn’t known to the compiler before this line. Though, struct A declaration is given after struct B declaration, compiler requires it before line

    struct A *a;

This is done using concept of Incomplete or partial Declaration. Let us see a simple program to understand mutually dependent structures.

advertisement
/*
 * mutuallydependant.c -- program declares mutually dependent structures
 * and access members of first using member of second which is a pointer
 * to first
 */
/* program uses concept of Incomplete declarations */
#include <stdio.h>
 
struct A;       /* Partial declaration for struct A */
 
struct B    {                               /* NEW tag for mem-list */
             int roll_no;
             float marks;
             struct A *p2A;     /* 'p2A' is a ptr-to-struct of type A */
        };
 
struct A    {
             float rain[5];
             struct B *p2B;     /* 'p2B' is a ptr-to-struct of type B */
             char initial;
        };
 
int main(void)
{
    int i = 0;
    struct B u;
    struct A a = {{1.1, 2.43, 2.01, 3.34, 2.213}, &u, 'A'};
    struct B b = {44, 58.0, &a};
 
    puts("Lets access members of structure \'a\' using pointer member "
         "\'p2A\', pointer to structure \'a\' and also a member of "
         "structure \'b\'\n");
 
    /* first member of structure 'a' is 'rain[5]' array */
    printf("First member of \'a\' is array \'rain of 5 floats\': ");
    for (i = 0; i < 5; i++)
        printf("%.2f ", (b.p2A)->rain[i]);
 
    puts("\n");
 
    /* second member of structure 'a' is pointer-to-structB */
    printf("Second member of structure \'a\' is a pointer to structure"
           " \'u\' of type struct B,\n");
    printf("Let us see the address of structure \'u\': ");
    printf("%p", (b.p2A)->p2B);
    puts("\n");
 
    printf("Final member of structure \'a\' is a character: ");
    printf("%c", (b.p2A)->initial);
    puts("\n");
 
    return 0;
}

Output of the program as follows,

Lets access members of structure 'a' using pointer member 'p2A',
pointer to structure 'a' and also a member of structure 'b'
 
First member of 'a' is array 'rain of 5 floats': 1.10 2.43 2.01 3.34 2.21
 
Second member of structure 'a' is a pointer to structure 'u' of type 
struct B, Let us see the address of structure 'u': 0x7fff9ce0c020
 
Final member of structure 'a' is a character: A

Notice that, in above program, we declared incomplete declaration for struct A before struct B declaration letting compiler know there exists struct A declaration somewhere in the program. And compiler now knows how much memory to allocate to pointer member of a struct B referring to struct A and pointer member of struct A referring to struct B. Can you guess what’d happen if we had used variables instead of pointer members in the declarations? Though, it’s simple, better you Try it out!

advertisement

Further, we accessed members of structure ‘a’ using pointer member ‘p2A’ of structure ‘b’ which is referring to structure ‘a’.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

If you find any mistake above, kindly email to [email protected]

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.