【问题标题】:How to transverse a linked list in Linux?如何在 Linux 中遍历链表?
【发布时间】:2018-01-29 01:54:52
【问题描述】:

所以我在 c 中有一个链表。我正在尝试使用 Linux 中的模块对其进行编译。所以在我的链表中,我输入了 6 个人。有月、日、年和名字。在程序中,我输入了 6 个人和属性。当我尝试横向链接列表时,它没有正确编译。我的代码几乎完成了,但不知道我在哪里搞砸了。

我收到的错误消息:

 implicit declartion of funciton 'print'
    assignment from incompatible pointer type
    expected ; before while
    ISO c90 forbids mixed declarations and code
    expected expression before',' token
    'next' undeclared
    invalid type argument of unary '*"

compiling through terminal and the erro i'm getting are: warning control reaches end non-void function. – black 7 mins ago   

Expted declaration or statement at end of input. – black 7 mins ago   

in expantion of macro 'Module_License – black 6 mins ago   

warning 'alias' attribute ignored. – black 5 mins ago   

Error: invalid storage class for function – black 5 mins ago   

error: expected identifier before '=' token *birthday_list->NULL; – black 5 mins ago   

error: struct birthday has no member named next – black 4 mins ago   edit           
You have a lot of mistake in your code such as ptr=&birthday_list (forgot semicolon in the end of the line. – ymonad just now

所以我试图让它到达输出的位置: It loads the module and states the six people then removes the people in the list.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>

struct birthday 
{   
    int month;
    int day;
    int year;
    char *name;

    struct list_head list;  
};

/**
 * The following defines and initializes a list_head object named birthday_list
 */
static LIST_HEAD(birthday_list);

int simple_init(void)
{
    struct birthday *person;
    /* Creating Person 1 */
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->month = 8;
    person->day = 12;
    person->year = 1993;
    person->name = "Aaron";
    INIT_LIST_HEAD(&person->list);

    list_add_tail(&person->list, &birthday_list);

    /* Creating Person 2 */
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->month = 4;
    person->day = 15;
    person->year = 1993;
    person->name = "Fish";

    INIT_LIST_HEAD(&person->list);
    list_add_tail(&person->list, &birthday_list);

    /* Creating Person 3 */
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->month = 3;
    person->day = 29;
    person->year = 1983;
    person->name = "js";

    INIT_LIST_HEAD(&person->list);
    list_add_tail(&person->list, &birthday_list);

    /* Creating Person 4 */
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->month = 7;
    person->day = 25;
    person->year = 1999;
    person->name = "Mark";

    INIT_LIST_HEAD(&person->list);
    list_add_tail(&person->list, &birthday_list);

    /* Creating Person 5 */
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->month = 7;
    person->day = 19;
    person->year = 1992;
    person->name = "Leah";

    INIT_LIST_HEAD(&person->list);
    list_add_tail(&person->list, &birthday_list);

    /* Creating Person 6 */
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->month = 1;
    person->day = 11;
    person->year = 1991;
    person->name = "Han";

    INIT_LIST_HEAD(&person->list);
    list_add_tail(&person->list, &birthday_list);

    printk(KERN_INFO "Loading Module\n");

    struct birthday *ptr;
    list_for_each_entry(ptr, &birthday_list, list) {
        /* On each iteration ptr points */
        /* to the next birthday struct  */
if(ptr ==NULL){
print("List is empty");
}else{
ptr=&birthday_list
while(ptr!=NULL){
ptr=ptr->list;
}
}


    return 0;
}

void simple_exit(void) {

    printk(KERN_INFO "Removing Module\n");
    struct birthday *ptr, *next

    list_for_each_entry_safe(ptr, next, *birthday_list, list) {
while(ptr!=NULL){
ptr->next=*birthday_list->next;
*birthday_list->=NULL;
}
        list_del(&ptr->list);
        kfree(ptr);
    }   
}

module_init(simple_init);
module_exit(simple_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel Data Structures");
MODULE_AUTHOR("SGG");

【问题讨论】:

  • 你是怎么编译的,错误信息是什么?
  • 通过终端编译,我得到的错误是:警告控制到达非无效函数。
  • 输入结束时的扩展声明或语句。
  • 您应该将错误消息粘贴到问题中,而不是作为评论。
  • 在“我收到的错误消息:”部分下的问题中,您会得到一个垃圾,例如 black 6 mins ago。从编译器的输出中复制实际错误消息,将其粘贴到问题帖子中并格式化为代码。

标签: c linux linked-list linux-kernel


【解决方案1】:

您的代码中有几个问题:

  • simple_exit 函数的第二行中,您忘记了行尾的;
  • simple_exit 函数中有ptr-&gt;next=*birthday_list-&gt;next; 行,其中ptr 是指向struct birthday 对象的指针。不幸的是,struct birthday 对象没有next 成员。
  • simple_exit 函数中有*birthday_list-&gt;=NULL; 行,这在语法上是错误的。你想写birthday_list = NULL; 或类似的东西吗?
  • simple_init 函数的末尾,您使用print 而不是printk
  • 您的编译器抱怨ISO c90 forbids mixed declarations and code。这基本上意味着,如果您使用符合 C90 标准的编译器,则必须在执行“其他任何操作”之前声明所有变量。所以像

    int i = 0;
    int j = 0;
    i = i + 5;
    

    工作时

    int i = 0;
    i = i + 5;
    int j = 0;
    

    不是因为j 是在“使用i 完成某些操作”之后声明的。你在你的代码中做类似的事情,例如你在simple_init函数的中间声明struct birthday *ptr;。在这种情况下,您应该将此行移至 simple_init 函数的开头。

代码中可能还有一些问题,但您可以先尝试解决这些问题。仔细阅读错误消息也是一个好主意,因为它们有时是不言自明的。

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 2014-12-14
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多