Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top