1
今天我收到了一个作业,约function pointer(binary tree)
。 有代码,我无法理解一大块......我明白了一个function pointer
是什么以及它是如何工作的,但怎么样2 function pointers
(1处于parameter
)指向结构函数指针的函数指针
header.h
typedef struct _node_ {
int key;
struct _node_ *left;
struct _node_ *right;
} node;
typedef struct _bstree_ {
node *root_node;
int (*compare_keys)(int x, int y); //this code
} bstree;
C文件
void bst_init_with_comp_operator(bstree *bst, int(*comp)(int x, int y)) {
bst_init(bst);
bst->compare_keys = comp; // what does this mean? a fucntion pointer parameter to function pointer to struct?
}
//排版应该返回-1,如果x应为小于y进行处理,零如果x应该输入代码here`被视为等于y,或1,如果x应该是视为大于y。
所以我创造了这个功能:
int compare (int x , int y)
{
if(x < y) return -1;
else if(x == y) return 0;
else return 1;
}
的main.c
bstree tree;
bst_init_with_comp_operator(&tree,compare(2,3))
,但它不工作...
通常,我们只需要像这样
int function(int x, int y)
{ return x+y;}
int (*pointerf) (int , int)
pointerf = function;
pointerf(2,3)
请定义“它不起作用”。它是否编译?它运行吗?它会崩溃吗?它擦除了您的硬盘驱动器? –
它给了我这个错误:警告:传递bst_init_with_comp_operator的参数2使得来自整型的指针没有强制转换 – Newuser1234567
这是因为您传递了“compare(2,3)”的结果而不是只是“compare”的函数。 – PaulR