【问题标题】:How do I compare length of two strings of two struct members?如何比较两个结构成员的两个字符串的长度?
【发布时间】:2020-10-08 00:58:04
【问题描述】:

我对 C 非常陌生。

我在尝试查找姓名最长的学生并将其从我的链接列表中删除时遇到问题。为此,我需要比较 comp *name; 的长度。对于结构的每个成员。

struct birthday{
    char *name;  
    ...
    struct list_head list;
};

LIST_HEAD(birthday_list);

int simple_init(void)
{
struct birthday *person;
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->name = "Alicia";
INIT_LIST_HEAD(&person->list);

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

    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->name = "Ben";
INIT_LIST_HEAD(&person->list);

list_add_tail(&person->list, $birthday_list);
    person = kmalloc(sizeof(*person), GFP_KERNEL);
    person->name = "Dave";
INIT_LIST_HEAD(&person->list);

list_add_tail(&person->list, $birthday_list);
...
struct birthday *ptr;
list_for_each_entry(ptr, &birthday_list, list)
{
    printk(KERN_INFO "Name: %s\n",
    ptr->name);
}
}

我正在寻找的是将 Alicia 与 Ben 进行比较,将 Alicia 存储为更长的名称,将 Alicia 与 Dave 进行比较,然后从链表中删除 Alicia 节点。我尝试了很多东西都无济于事。如果有人有

【问题讨论】:

  • 欢迎来到 SO。如果您“尝试了很多事情”,最好向我们展示这些努力。然后我们就可以知道出了什么问题。您显示的代码不包含任何尝试执行您要求的代码。

标签: c struct linked-list doubly-linked-list


【解决方案1】:

对于字符串比较,请尝试strlen()

size_t strlen ( const char * str );

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多