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
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
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.
/* * 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!
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.
- Practice BCA MCQs
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Check C Books