“->”的无效类型参数

invalid type argument of '->'

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/module.h>

int start(void){

    printk("starting module\n");
    printk("pid\ttask_name\tstate\n");

    struct task_struct *task;
    struct list_head *list;

    list_for_each(list,&init_task->children){
        task=list_entry(list,struct task_struct,sibling);
        printk("%5d\t%s\t%li\n",task->pid,task->comm,task->state);
    }
    return 0;
}


void ending(void){
    printk("closing module\n");
}

module_init(start);
module_exit(ending);

编译时出现如下错误

error: invalid type argument of ‘->’ (have ‘struct task_struct’)
  list_for_each(list,&init_task->children){
                               ^
include/linux/list.h:408:14: note: in definition of macro ‘list_for_each’
  for (pos = (head)->next; pos != (head); pos = pos->next)

我认为错误信息试图说明“&init_task”应该有 "struct task_struct" 但这还不够吗?据我所知,变量 'init_task' 是在 'linux/sched.h' 中外部声明的,这意味着错误不应该是由于无法定位或找不到 'init_task'.

而引起的

因为指针应该放在'->'前面,所以使用'&init_task'似乎是正确的。

有人可以指出我在这里遗漏了什么吗?

---更新:已解决---

重新考虑“->”和“&”的运算符优先级,正确的用法是“(&init_task)->children”或'init_task.children'。但是,即使进行了此更改,仍会出现错误:

include/linux/list.h:408:31: error: invalid operands to binary != (have ‘struct list_head *’ and ‘struct list_head’)
  for (pos = (head)->next; pos != (head); pos = pos->next)

此错误消息指出我应该在 'struct task_struct' 中提供 'children' 的地址。因此,我将有问题的行更改为:

list_for_each(list,&(init_task.children)){

问题解决,编译顺利

init_task是结构体,不是指针。因此,您应该在取消引用 (&init_task)->children 之前将其转换为指针,或者使用句点符号 init_task.children 访问 children&init_task->children 表示 init_task 指向的结构的 children 字段的地址,如果它是指针。