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
};

但是我不确定是否可以分别声明和初始化静态变量

有没有一种方法可以使我从编译中真正指向彼此?

最佳答案

你快到了您需要先“静态声明”(实际上,这是临时定义,感谢AndreyT!)静态实例,然后使用所需的指针初始化它们的定义。

static struct thingy thing1;
static struct thingy thing2;

static struct thingy thing1 = {
    .pointer = &thing2
};

static struct thingy thing2 = {
    .pointer = &thing1
};

从技术上讲,您只需要转发声明就可以临时定义thing2

10-04 21:13