Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I allowed to make circular references with constants structs?

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.

like image 547
Mr. Minty Fresh Avatar asked Sep 18 '17 23:09

Mr. Minty Fresh


2 Answers

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
};
like image 180
ldav1s Avatar answered Oct 01 '22 19:10

ldav1s


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.

like image 39
Sergey Kalinichenko Avatar answered Oct 01 '22 20:10

Sergey Kalinichenko