r/cprogramming • • Jul 06 '26

anonymously initializing pointers in self-referential data-structures?

I have a recursive data-structure (a simple linked list for purposes of this example) and wanted to statically define a linked-list. The following works fine:

#include <stdio.h>
typedef struct mytype_tag {
    struct mytype_tag* next;
    char* data;
} mytype;

mytype a = {
    .next = NULL,
    .data = "a",
};
mytype b = {
    .next = &a,
    .data = "b",
};

int
main() {
    mytype* s = &b;
    int i = 0;
    while (s) {
        printf("%d: %s\n", i++, s->data);
        s = s->next;
    };
}

However, I have to explicitly define/declare a and then have b take &a.

Is there a way to do this with anonymous/unnamed intermediary structures, thinking an imaginary syntax something like

mytype b = {
    .next = &((mytype)={
        .next = NULL,
        .data = "a",
        }),
    .data = "b",
};

so I can build up the linked-list without naming each intermediary instance?

6 Upvotes

12 comments sorted by

3

u/sciencekm Jul 06 '26

You can create an array.

In the following example, I created a circular list and printed them.

#include <stdio.h>
typedef struct snode { struct snode *nxt; char *s; } node_t;
node_t nodes[3] = {
{ nodes + 1, "1st" },
{ nodes + 2, "2nd" },
{ nodes + 0, "3rd" } };
int main(void) {
node_t *start = nodes, *p = start;
do printf ("%s\n", p->s); while((p = p->nxt) != start);
return 0;
}

0

u/[deleted] Jul 06 '26

[deleted] — view deleted comment

1

u/sciencekm Jul 06 '26

A linked list can grow. An array cannot. If you want an initialized linked list, this is one way to do it. Then later, at run time, it could grow.

0

u/[deleted] Jul 06 '26

[deleted] — view deleted comment

1

u/sciencekm Jul 06 '26

If you start from zero, that means that you need an initialization code at startup. This is OK if you own main(). What if yours is a library. You now have to expose an init() function and require all users of the library to call it before any other calls to the library.

I'm not saying this is what the OP is doing. I could be wrong. I'm just saying that there are valid scenarios for statically initialized objects, including linked lists.

0

u/[deleted] Jul 06 '26

[deleted] — view deleted comment

1

u/sciencekm Jul 06 '26

Yes that would also do it.

I my self am partial to avoiding code just to initialize anything. I don't want to call add_to_list X times just to initialize.

Also, look at the binary size. Statically initializing objects result in smaller binary vs using code.

1

u/[deleted] Jul 06 '26

[removed] — view removed comment

1

u/gumnos Jul 06 '26

in this case, it was "there's some code that handles linked lists properly for other dynamic areas of code, but I have a few hard-coded lists that I want the code to handle in the same way"

1

u/FISHARM1 Jul 06 '26

I believe you can just do .next = (mytype){.next = Null, .data = “a”}

1

u/gumnos Jul 06 '26

yeah, it needs the & to address the static structure, but it was my = that I stuck in there that was causing the problems. Once I got the syntax correct, it worked like a charm. Thanks!

1

u/[deleted] Jul 06 '26

[removed] — view removed comment

1

u/gumnos Jul 06 '26

there's something to be said for the easier-to-read aspect (allowing it to be forward rather than reverse menuing), but in some of the actual underlying use-cases, they can get built out at compile-time, meaning it's more challenging to invoke item-building-functions.

Fortunately, what I wanted is possible…thanks to u/thegreatunclean who identified that I was close, but just needed to remove the = for it to be properly handled by the compiler.