1

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So in this case, foo represents a single object in the list? And what sort of parameters will LIST_HEAD() take? I'm not sure what you mean by "this_is_a_list_instance"
so LIST_HEAD(this_is_a_list_list_instance) is a macro that is going to get converted into struct list_head this_is_a_list_instance = .... So I'm using that as a name for an object.
Understood. Thanks for sticking with me. I have updated my question at the top.
Fixed by moving LIST_HEAD() outside of main(). Haha, oh well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.