c - 如何从不兼容的指针类型中修复传递“count”的参数 1
问题描述
我正在尝试制作一个带有链表的 C 语言程序,该链表使用递归来计算链表节点的总数。但是,我收到此错误:
passing argument 1 of 'count' from incompatible pointer type [-Wincompatible-pointer-types]|
谁能向我解释为什么会发生这种情况以及如何解决?
这是没有递归的链表节点。它可以正常工作并返回我需要的值。
int count(list_t *list){
node_t *curr = list->head;
int length=0;
while(curr != NULL){
length++;
curr = curr->next;
}
return(length);
}
这个是递归的,但我从不兼容的指针中得到“'count'”错误。
int count(list_t *list){
node_t *curr = list->currptr;
int length=0;
if(curr == NULL){
return(0);
}
return(1 + count(curr->next));
}
这是我的链表的结构
typedef struct {
int yyyy, mm,dd;
} date_t;
typedef struct {
double balance;
} acc_balance;
typedef struct node node_t;
struct node{
char *acc_no, *name, *lastname;
date_t date;
acc_balance acc_balance;
node_t *next;
};
typedef struct {
node_t *head;
node_t *foot;
node_t *currptr;
} list_t;
输出应为 8,但程序以以下方式终止:
Process returned -1073741819 (0xC0000005) execution time : 5.093 s
我还是 StackOverflow 的新手。如果我说错了,我很抱歉。
解决方案
感谢您发布定义。这很有帮助。对于递归,您只需要处理node_t
而不是list_t
. 它可能看起来像这样:
int count(node_t *curr)
{
if (curr == NULL)
{
return (0);
}
return (1 + count(curr->next));
}
然后你这样称呼它:
count(list->head);
如果您希望能够使用 a 调用它list_t
,则添加一个辅助函数,如下所示:
int countList(list_t *list)
{
return count(list->head);
}
推荐阅读
- javascript - Linq js group by Sum 附加字符串而不是添加
- azure - Cosmos DB:如何使用 LINQ 查询检测请求费用
- python - 有没有一种有效的方法可以将多维字典转换为 python 中的列表?
- c# - 用于 C# .NET Core 中包引用的用户定义条件变量?
- jsonpath - 如何在 Angular 2+ 中使用功能齐全的 JsonPath?
- swift - 斯威夫特如何制作一个只有在持续点击几秒钟后才会采取行动的按钮
- r - 点到固定目标点的平均距离
- xamarin.forms - 获取使用的 Api flurl.http 方法
- vba - 用于创建数据透视表的 VBA 代码可以正常工作,但现在不行
- node.js - 在 google App Engine 上部署:发生错误:ffmpeg 被信号 SIGABRT 杀死错误:ffmpeg 被信号 SIGABRT 杀死