如何在Linux中横切链表?

时间:2018-01-29 01:54:53

标签: c linux linked-list linux-kernel

所以我在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");

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题:

  • simple_exit函数的第二行,您忘记了行尾的;
  • simple_exit函数中,您有ptr->next=*birthday_list->next;行,其中ptr是指向struct birthday对象的指针。遗憾的是,struct birthday个对象没有next个成员。
  • simple_exit函数中,您有*birthday_list->=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完成某事后”而宣布。您在代码中执行类似的操作,例如在struct birthday *ptr;函数的中间声明simple_init。在这种情况下,您应该将此行移至simple_init函数的开头。

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