A.h
#ifndef A
#define A
#include "B.h"
typedef struct {
B* b;
} A;
void InitA(A* a) {
a->b=malloc(sizeof(B));
}
#endif
B.h
#ifndef B
#define B
#include "A.h"
typedef struct {
A* a;
} B;
void InitB(B* b) {
b->a=malloc(sizeof(A));
}
#endif
I tried like that ,like c++ and typedef.
typedef makes conflict errors between types and previous declare was here and etc.
Thanks.
Here’s one way of fixing it:
a.h:
b.h:
main.c:
The problem is, for
sizeof()to work, its parameter must be of a known type. In your codeAandBare not yet fully known when you dosizeof(A)andsizeof(B)inInitA()andInitB(). Rearranging the order of the type definitions, file inclusion and the function definitions can fix that.