【发布时间】:2021-11-09 18:16:46
【问题描述】:
当我将宏之类的函数作为参数传递给另一个声明为函数指针参数的函数时。我无法运行出现编译时错误的代码。
#include <stdio.h>
#define print_numbers() (void (0))
void display(void (*p)())
{
for(int i=1;i<=5;i++)
{
p();
}
}
int main() {
void (*p)(int); // void function pointer declaration
printf("The values are :");
display(print_numbers); return 0;
}
错误
prog.c: In function ‘main’: prog.c:16:13: error: ‘print_numbers’ undeclared (first use in this function)
display(print_numbers);
^
prog.c:16:13: note: each undeclared identifier is reported only once for each function it appears in
【问题讨论】:
-
阅读How to Ask 并获取tour。
-
你不能在 C 中做到这一点。
-
这看起来真的是一个 XY 问题。你想达到什么目的?
-
请注意,宏的扩展是无效的 C 除非关键字
void有#define。请注意,display()的参数不是对类函数宏的调用。
标签: c function compiler-errors