C Circular Doubly Linked List with static head initialization of next/prev pointers to static head address

I want to initialize a Circular Doubly Linked List next/previous pointers to point at the address of a statically allocated element used as sentinel/first element. In this way, I can simplify the list operations. I tried the following code:

struct ErrorDataElement {
    struct ErrorDataElement *next;
    struct ErrorDataElement *prev;
    enum ErrorType type;
    int code;
    char **messages;
    char *file;
    char *function;
    int line;
} _errorList = { &_errorList, &_errorList, 0, -1, NULL, NULL, NULL, 0 };

When I print &_errorList memory address (in a sub-function called from main() function) I get a different address from _errorList.next and _errorList.prev.

_errorList.next and _errorList.prev have coherently the same value.

If I inspect using the following code:

printf("%d %d", _errorList.code, _errorList.next->code);

I see that _errorList.code = -1 as expected, while _errorList.next->code = 1001, indicating to me that the value of _errorList.next has been initialized to some incorrect memory address. I suspect this is due to relative/absolute memory addressing during function initialization, but I can't figure out exactly and how to correctly initialize the static element, or whether or not is even possible such initialization.

Read more here: https://stackoverflow.com/questions/64943413/c-circular-doubly-linked-list-with-static-head-initialization-of-next-prev-point

Content Attribution

This content was originally published by massi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: