Initialize linked list with linux/list.h
-
07-03-2021 - |
Pregunta
I am using linux/list.h to work with lists, but I am having trouble getting started.
I having been experimenting with different combinations based on a few guides' recommendations, but nothing has been working so far. Here's what I believe to be my closest attempt at initialization:
typedef struct {
int to;
struct list_head list;
int from;
} myFrame;
int main() {
LIST_HEAD(listInstance);
myFrame* foo = malloc(sizeof(*foo));
list_add(&foo->list, &listInstance);
}
This produces the warning
initializer element is not computable at load time
at LIST_HEAD()
However, this seems to indicate that I can.
Alternatively, lists can be initialized at compile time
Would someone mind helping me wrap my brain around this?
Solución
You actually want to do:
LIST_HEAD(this_is_a_list_instance);
You can then do:
struct myList *foo = malloc(sizeof(*foo));
foo->to = 3;
foo->from = 4;
list_add(&foo->list, &this_is_a_list_instance);
This would also probably be more consistent if you named it myObject
instead of myList
.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow