C 允许我使用常量结构进行循环引用吗?

C 允许我使用常量结构进行循环引用吗?,c,struct,circular-reference,C,Struct,Circular Reference,我可以在家里做这件事吗 我可以让我的程序没有这个工作。这很方便。你可以。您只需向前声明tail即可使其正常工作: typedef struct dlNode { struct dlNode* next; struct dlNode* prev; void* datum; } dlNode; const static dlNode tail; const static dlNode head={ .next = &tail, .prev = NU

我可以在家里做这件事吗


我可以让我的程序没有这个工作。这很方便。

你可以。您只需向前声明
tail
即可使其正常工作:

typedef struct dlNode {
    struct dlNode* next;
    struct dlNode* prev;
    void* datum;
} dlNode;

const static dlNode tail;

const static dlNode head={
    .next = &tail,
    .prev = NULL,
    .datum = NULL
};

const static dlNode tail={
    .next = NULL,
    .prev = &head,
    .datum = NULL
};

您完全可以这样做:添加一个前向声明
tail
,C将它与后面的定义合并:

typedef struct dlNode {
    const struct dlNode* next, *prev;
    void* datum;
} dlNode;

const static dlNode tail; // <<== C treats this as a forward declaration

const static dlNode head={
    .next=&tail,
    .prev=NULL,
    .datum=NULL
};

const static dlNode tail={ // This becomes the actual definition
    .next=NULL,
    .prev=&head,
    .datum=NULL
};
typedef结构dlNode{
const struct dlNode*next,*prev;
无效*基准;
}dlNode;

常量静态dlNode tail;//谢谢,我不知道你能(有效)原型变量和结构。注意这是所谓的“暂定定义”,在C++中不起作用。@ O11C,这个模式很容易被类替换。@ BunyAcLoVin并不意味着,在C++中,特别是在C++中,临时定义不具体工作。拍击
类foo{}foo_实例将使其编译为静态存储中的只读数据,即使没有优化。
typedef struct dlNode {
    const struct dlNode* next, *prev;
    void* datum;
} dlNode;

const static dlNode tail; // <<== C treats this as a forward declaration

const static dlNode head={
    .next=&tail,
    .prev=NULL,
    .datum=NULL
};

const static dlNode tail={ // This becomes the actual definition
    .next=NULL,
    .prev=&head,
    .datum=NULL
};