问题描述
我正在为二进制树的一组方法编写代码并收到了这个编译错误:
pfbst.c:在函数'main中':
pfbst.c:65:13:警告:从不兼容的指针类型[默认启用]传递'bst_char_iterate'的参数2
bst_char_iterate(t,print_char) ;
^
pfbst.c中包含的文件:6:0:
pfbst_char.h:26:6:注意:预期' char(*)(char)'但参数的类型为'void(*)(char)'
int bst_char_iterate(bst_char * t,char(* fun)(char item));
^
这是代码:
I am writing code to a set of methods for a binary tree and have received this compilation error:
pfbst.c: In function ‘main’:
pfbst.c:65:13: warning: passing argument 2 of ‘bst_char_iterate’ from incompatible pointer type [enabled by default]
bst_char_iterate(t,print_char);
^
In file included from pfbst.c:6: 0:
pfbst_char.h:26:6: note: expected ‘char (*)(char)’ but argument is of type ‘void (*)(char)’
int bst_char_iterate(bst_char *t, char (*fun)(char item));
^
Here's the code:
int bst_iterate(struct node *p, char (*fun)(char item)) {
if (p!=NULL) {
p->item = fun(p->item);
bst_iterate(p->left,fun);
bst_iterate(p->right,fun);
} else {
return 0;
}
}
int bst_char_iterate(bst_char *t, char (*fun)(char item)) {
assert(t!=NULL);
bst_iterate(t->root,fun);
}
推荐答案
bst_char_iterate(t,print_char);
部分导致错误。您的 print_char
函数签名与错误中的预期值不同。
part causes the error. Your print_char
function signature differs from the expected one as in the error.
pfbst_char.h:26:6:注意:预期'char(*)(char)'但参数类型为'void(*)(char)'
pfbst_char.h:26:6: note: expected ‘char (*)(char)’ but argument is of type ‘void (*)(char)’
如果将 print_char
的返回类型更改为char并且在函数中返回一个char,编译器将停止抱怨:)
If you change print_char
's return type to char and return a char in the function, the compiler will stop complaining :)
char print_char(char c) {
return 0;
}
这篇关于指针类型不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!