You need to understand the difference between a declaration and definition. For the moment, I put typedef
aside. In order to be able to do this definition:
struct A {
struct B *b;
};
you must FIRST declare struct B
. Note that if you have declared something before defining it, the definition counts both as a definition and a declaration. But in this case, because of the circular dependency, we need separate declarations. You can solve that with:
struct A;
struct B;
Those two lines basically says "it exists two structs, and their names are A and B".
In most cases, the preferred solution would be something like this:
a.h
#ifndef A_H
#define A_H
typedef struct A A;
#endif
Similar for b.h
a.c
#include "a.h"
#include "b.h"
struct A {
struct B* B;
};
Note, do not use different names for the struct and the typedef unless you can come up with a good reason not to. (I bet you cannot) and if you decide to do that anyway, it's not advisable to start with an underscore, because those are reserved for future use.
I wrote a related answer on this topic: https://stackoverflow.com/a/54752982/6699433