c - Richard F. Gilberg 的数据结构书中的 createlist 函数声明在 c 中的正确转换是什么
问题描述
这是功能
*/
LIST* createList (int (*compare) (void* argu1, void* argu2)) {
//Local Definitions
LIST* list;
//Statements
list = (LIST*) malloc (sizeof (LIST)); if (list)
{
list->head = NULL;
list->pos = NULL;
list->rear = NULL;
list->count = 0;
list->compare = compare; } // if
return list;
} // createList
LIST* createList (int (* compare) (void* argu1, void* argu2)) 我的尝试 Createlist 是一个需要参数的函数(compare 是一个指向函数的指针,需要泛型指针并返回整数)返回 * List
解决方案
createList
是一个具有一个函数指针参数并返回指向 的指针的函数LIST
。
参数的类型是“指向函数的指针,带有 (void*, void*) 参数并返回 int ”
大概你有一个函数,比如:
int my_comp(void* argu1, void* argu2) // this matches the type of the createList parameter
{
// do something to compare *argu1 and *argu2
return 0; // or some other integer value
}
然后你调用createList
,传递一个指向my_comp
:
LIST* my_list = createList(my_comp); // this passes the address of my_comp to createList