void sortRecords(char* records[], int size, int isGreater(const char rec1[],
const char rec2[]));
int isGreaterByName(const char record1[], const char record2[]);
int isGreaterByCity(const char record1[], const char record2[]);
int isGreaterByEmail(const char record1[], const char record2[]);
其实我不知道如何搜索(甚至不知道如何调用)..我需要知道如何使用这种类型的功能。
我将这些作为我的函数原型(prototype)。我需要这个函数的示例用法:)
我试过了
char eMail[30];
sortRecords(addresses,30,isGreaterByName(eMail,eMail));
但是编译器给了我
In function 'main':|
|69|error: passing argument 3 of 'sortRecords' makes pointer from integer without a cast|
|50|note: expected 'int (*)(const char *, const char *)' but argument is of type 'int'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
对不起我的英语不好^.^
最佳答案
当你传递一个函数指针时,你省略了括号和参数:
sortRecords(addresses, 30, isGreaterByName);
当您包含括号和参数时,编译器调用函数并将返回值(通常不是函数指针)传递给需要函数指针的函数,从而导致所示问题。
您使用的是 GCC 的现代版本,它会尽力告诉您哪里出了问题:
expected 'int (*)(const char *, const char *)' but argument is of type 'int'
函数的返回值是一个int
;预期的类型是 int (*)(const char *, const char *)
这就是您如何将强制转换写入函数指针的方式。最终,您需要学习该表示法。
关于c - 函数原型(prototype)中的函数声明(需要帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270289/