I have been given a function interface that looks like this:
typedef enum order (*cmp_fun)(const void *x, const void *y);
bool tree_member(cmp_fun, const void *, const struct tree *);
const struct tree *tree_insert(cmp_fun, const void *, const struct tree *);
When I try to give the function pointer a name and implement the function I get a compiler error and I'm not sure what I'm doing wrong:
bool tree_member(order (*funcptr), const void* test_value, const struct tree* my_tree){... ///?
const struct tree* tree_insert(int (*funcptr), const void* insert_value, const struct tree* my_tree){... //???
There is something about the function declaration for accepting the function pointer as a parameter that I am not understanding
In file included from tree-examples.c:21:
tree.c:15:18: error: unknown type name ‘order’
15 | bool tree_member(order (*funcptr), const void* test_value, const struct tree* my_tree){
| ^~~~~
tree.c:22:20: error: conflicting types for ‘tree_insert’
22 | const struct tree* tree_insert(int (*funcptr), const void* insert_value, const struct tree* my_tree){
| ^~~~~~~~~~~
I have tried many many variations:
bool tree_member(cmp_fun (*funcptr), const void* test_value, const struct tree* my_tree){
const struct tree* tree_insert(cmp_fun (*funcptr), const void* insert_value, const struct tree* my_tree){
CodePudding user response:
The compiler is telling you that it doesn't recognize order
as a type name. You need to write enum order
instead of just order
.
Looking at that part of the code, I noticed you used the wrong type for the first argument of the tree_member
function. The interface is telling you that the first argument of tree_member
must have type cmp_fun
, but you accidentally made it have type order *
.
The compiler is also telling you that the tree_insert
implementation you wrote has the wrong type. Look carefully at the first argument of tree_insert
. It is supposed to be a cmp_fun
according to the interface, but you accidentally made your first argumet be an int *
instead.
Hint: Instead of typing your function implementations in from scratch, you can simply copy the declaration you were given, replace ;
with { }
, and add a name for any arguments that are missing a name. Following this hint, you would get this code as a starting point:
bool tree_member(cmp_fun cmp, const void * ptr,
const struct tree * my_tree)
{
}
const struct tree * tree_insert(cmp_fun cmp, const void * ptr,
const struct tree * my_tree)
{
}