c - 将函数作为参数传递,带或不带 & 符号 c

标签 c function parameters

我想传递一个函数作为参数,但我很困惑是否应该用&符号传递它。以下代码片段以两种方式起作用。为什么?

#include <stdio.h>

int print(int pas){
return pas;
}


int func_print(int (*k)(int)){
(*k)(555);
}

int main(){



printf("%d",func_print(print));
printf("%d",func_print(&print));

return 0;
}

最佳答案

函数名称很特殊。

当在不调用函数的情况下使用时,它会自动转换为指向函数的指针。所以 &printprint*print**print***print > 等都计算为 int (*)(int) 类型的相同表达式。

关于c - 将函数作为参数传递,带或不带 & 符号 c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969619/

相关文章:

c - 使用 cJSON 库解析 json 数组

c++ - 在 int main() [c++] 中实现递归函数

arrays - 有没有办法创建一个可以接受数组和范围作为输入的 VBA 函数?

php - 无法使用 PDO 将数据插入数据库

c++ - 形式参数列表不匹配

c - 内存不足..怎么办?

c++ - C/C++ 不确定值 : Compiler optimization gives different output (example)

c - 防止 valgrind 检查与我们的应用程序链接的共享库中的内存泄漏

python - 如何使用if条件捕获函数返回值

PowerShell 函数参数 : Can the first one be optional first?