gpt4 book ai didi

c - Linux中如何遍历链表?

转载 作者:行者123 更新时间:2023-11-30 20:19:51 25 4
gpt4 key购买 nike

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

最佳答案

您的代码中有几个问题:

  • simple_exit 函数的第二行中,您忘记了行尾的 ;
  • simple_exit 函数中,有行 ptr->next=*birthday_list->next; ,其中 ptr 是指向结构生日对象。不幸的是,structbirthday对象没有next成员。
  • simple_exit 函数中,有行 *birthday_list->=NULL; ,这在语法方面是错误的。您想编写 birthday_list = NULL; 或类似的内容吗?
  • simple_init 函数末尾,您使用 print 而不是 printk
  • 您的编译器提示 ISO c90 禁止混合声明和代码。这基本上意味着,如果您使用遵守 C90 标准的编译器,则必须在执行“其他操作”之前声明所有变量。所以类似

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

    工作时

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

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

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

关于c - Linux中如何遍历链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com