C语言如何实现类似于C++中前向引用声明的功能,想在C语言中写两个结构,A和B,结构A中有指向结构B的指针,结构B中有指向结构A的指针,这样可以么
可以,代码奉上
#include<stdio.h>
struct nodeB;
struct nodeA
{
int value;
struct nodeB* ptrB;
};
struct nodeB
{
char value;
struct nodeA* ptrA;
};
int main()
{
struct nodeA A={1,NULL};
struct nodeB B={'a',NULL};
A.ptrB=&B;
B.ptrA=&A;
printf("%d %c %d",A.value,A.ptrB->value,A.ptrB->ptrA->value);
return 0;
}
可以见3个结构体,A和B,C是他们互相指向的指针,
typedef sturct A
{
int dataA;
}A_t;
typedef struct B
{
int dataB;
}B_t;
typedef struct C
{
A_t *x;
B_t *y;
}C_t;