c - 我如何测试这个函数,它会从列表中删除所有元素,其数据“等于”参考数据
问题描述
我有这个函数,我想测试它,看看它是否能完成工作,但它没有返回,当我尝试将其返回类型更改为 (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
解决方案
推荐阅读
- javascript - 如何解决 Web Audio API 的问题:DOMException: Unable to decode audio data
- linux - 为什么我必须将 .exe 放在文件名之后才能从命令行执行?
- r - 在 R 中将 flextable 对齐/拟合到页面宽度
- javascript - 在映射数据上显示对象的 dangerouslySetInnerHTML
- ruby - Olog(n) 在文件中搜索
- reactjs - Reactjs如何显示我从api获得的数组数据?
- typescript - VueJS中的地图函数内的“对象类型未知”
- database - 使用 CouchbaseCLI 导出数据 cbexport 错误
- formio - 如何在 form.io 编辑器中为组件添加默认参数
- asp.net - 指数超出范围。必须是非负数且小于集合的大小。参数名称:VB.Net中的索引