Mr.*_*esh 11 c struct circular-reference
我可以在C99这样做吗?
typedef struct dlNode {
dlNode* next,prev;
void* datum;
} dlNode;
const static dlNode head={
.next=&tail,
.prev=NULL,
.datum=NULL
};
const static dlNode tail={
.next=NULL,
.prev=&head,
.datum=NULL
};
Run Code Online (Sandbox Code Playgroud)
如果没有这个,我可以让我的程序工作,它只是方便.
lda*_*v1s 15
你可以,你只需要转发声明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
};
Run Code Online (Sandbox Code Playgroud)
绝对允许你这样做:添加一个前向声明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
};
Run Code Online (Sandbox Code Playgroud)
请注意,您应该将struct声明修复为make next和prevconstant,否则您的定义将丢弃常量限定符.