C语法问题。
我希望做出如下声明:
struct thingy {
struct thingy* pointer;
}
static struct thingy thing1 = {
.pointer = &thing2
};
static struct thingy thing2 = {
.pointer = &thing1
};
我已尝试单独声明和初始化,如:
struct thingy {
struct thingy* pointer;
}
static struct thingy thing1;
static struct thingy thing2;
thing1 = {
.pointer = &thing2
};
thing2 = {
.pointer = &thing1
};
但是我不确定我是否可以单独声明和初始化静态变量
我有没有办法让这些从编译中指向对方?
答案 0 :(得分:3)
你快到了。你需要"转发声明" (实际上,这是 暂定定义 ,感谢AndreyT!)首先是静态实例,然后使用所需的指针初始化它们的定义。
static struct thingy thing1;
static struct thingy thing2;
static struct thingy thing1 = {
.pointer = &thing2
};
static struct thingy thing2 = {
.pointer = &thing1
};
从技术上讲,你只需要转发声明暂时定义thing2
。