Am I allowed to make circular references with constants structs?
You are absolutely allowed to do it: add a forward declaration of tail
, and C will merge it with a later definition:
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
};
Note that you should fix your struct
declaration to make next
and prev
constant, otherwise your definition would discard constant qualifiers.
Demo.
You can. You just have to forward declare tail
to get it to work:
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
};