我正在使用 linux/list.h 来处理列表,但我在开始时遇到了问题。
我一直在根据一些指南的建议尝试不同的组合,但到目前为止没有任何效果。这是我认为最接近的初始化尝试:
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);
}
这会产生警告
initializer element is not computable at load time
在 LIST_HEAD()
然而,this似乎表明我可以。
Alternatively, lists can be initialized at compile time
有人介意帮我解决这个问题吗?
最佳答案
你实际上想做的是:
LIST_HEAD(this_is_a_list_instance);
然后你可以这样做:
struct myList *foo = malloc(sizeof(*foo));
foo->to = 3;
foo->from = 4;
list_add(&foo->list, &this_is_a_list_instance);
如果您将其命名为 myObject
而不是 myList
,这也可能会更加一致。
关于c - 用 linux/list.h 初始化链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8236812/