c - 函数原型(prototype)中的函数声明(需要帮助)

标签 c function prototype

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/

相关文章:

c++ - malloc 分配给指针

r - 如何在加载包时完全防止 "The following object is masked from ‘package:...’ :. .."问题/警告?

c++ - 为什么它试图发送一个字符数组而不是一个字符串?

javascript - Object.create(null) 的用例

内部带有 UIControl 的 iOS 原型(prototype)单元格

c++ - 我们可以有递归宏吗?

C 大型二维数组创建

c - 如何使用结构创建 char** 类型的变量

javascript - 是否可以用自定义函数覆盖 window.location 函数?

C++ 函数原型(prototype) a\And 变量名与仅数据类型