首页 > 解决方案 > 我如何测试这个函数,它会从列表中删除所有元素,其数据“等于”参考数据

问题描述

我有这个函数,我想测试它,看看它是否能完成工作,但它没有返回,当我尝试将其返回类型更改为 (t_list*) 并使其返回 (node) 时,编译器给了我分段错误

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct  s_list
{
    struct s_list *next;
    void    *data;
}  t_list;


void    ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)())
{
    t_list *root;
    t_list *node;
    t_list *next;

    root = *begin_list;
    node = *begin_list;
    while (root)
    {
        next = (*root).next;
        if ((cmp)(root->data, data_ref) == 0)
        {
            if (root == *begin_list)
            {
                *begin_list = next;
            }
            node->next = next;
            free(root);
        }
        node = root;
        root = next;
    }
}

主要是我创建了“data_ref”并制作了5个结构,其中一些具有“data_ref”并将它们链接在一起,但是当我调用该函数时,它给了我分段错误

t_list *res;
res = ft_list_remove_if(ptr, str, strcmp); 

ptr 具有列表头的地址,而 str 具有 data_ref

标签: c

解决方案


推荐阅读