list_for_each 教程中“struct”之前的预期表达式

问题描述

该行出现以下错误 expected expression before 'struct'

struct my_obj *obj = list_entry(i,struct my_obj,node);

在下面代码底部

struct my_container {
    struct list_head list;
    int some_member;
    /* etc. */
};

struct my_obj {
    struct list_head node;
    int some_member;
    /* etc. */
};

void func() {
    struct my_container container;
    struct my_obj obj1,obj2;
    struct list_head *i;

    /* INIT_LIST_HEAD(&container.list); */
    container.list.next = &container.list;
    container.list.prev = &container.list;

    /* list_add_tail(&obj1.node); */
    container.list.prev = &obj1.node;
    obj1.node.next = &container.list;
    obj1.node.prev = &container.list;
    container.list.next = &obj1.node;

    /* list_add_tail(&obj2.node); */
    container.list.prev = &obj2.node;
    obj2.node.next = &container.list;
    obj2.node.prev = &obj1.node;
    obj1.node.next = &obj2.node;

    /* list_for_each(i,&container.list) { */
    for (i = container.list.next; i != &container.list; i = i->next) {
        struct my_obj *obj = list_entry(i,node);
        /* do stuff */
    }

}

请您在部分中的struct之前告知它想要什么 list_entry(i,node);

解决方法

抑制函数参数中的'struct':

struct my_obj *obj = list_entry(i,struct my_obj,node);

->

struct my_obj *obj = list_entry(i,my_obj,node);