Am I allowed to do this in 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
};
I can make my program work without this. It'd just be convenient.
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
};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With