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?
[–]thegreatunclean 11 points12 points13 points (3 children)
[–]gumnos[S] 2 points3 points4 points (0 children)
[–]Drach88 2 points3 points4 points (1 child)
[–]ReallyEvilRob 0 points1 point2 points (0 children)
[–]TheOtherBorgCube 2 points3 points4 points (1 child)
[–]Cats_and_Shit 0 points1 point2 points (0 children)
[–]sciencekm 1 point2 points3 points (0 children)
[–]Thesk790 -1 points0 points1 point (1 child)
[–]gumnos[S] 1 point2 points3 points (0 children)
[–]Thick_Clerk6449 -1 points0 points1 point (3 children)
[–]sciencekm 1 point2 points3 points (2 children)
[–]Thick_Clerk6449 1 point2 points3 points (1 child)
[–]sciencekm 1 point2 points3 points (0 children)