在 C 中的另一个函数的输入中定义一个函数
Defining a function inside the input of another function in C
我有一个函数,它有一个函数指针输入。我可以很容易地为它提供函数名称作为输入。但我想知道是否可以将函数定义为输入。例如我有一个这样的函数;
void exampleFunction ( void (*functionPointer)( void ) ) {
codes
...
}
我可以在括号内给出这样的输入吗?例如;
exampleFunction( void helloFunction ( void ) {
printf("Hello");
} );
它会给出这样的编译错误,但是还有其他方法吗?
这在 C 中是不可能的。
在 C++ 中,您可以使用 lambda 表达式:
exampleFunction([](){ std::cout << "Hello"; });
我有一个函数,它有一个函数指针输入。我可以很容易地为它提供函数名称作为输入。但我想知道是否可以将函数定义为输入。例如我有一个这样的函数;
void exampleFunction ( void (*functionPointer)( void ) ) {
codes
...
}
我可以在括号内给出这样的输入吗?例如;
exampleFunction( void helloFunction ( void ) {
printf("Hello");
} );
它会给出这样的编译错误,但是还有其他方法吗?
这在 C 中是不可能的。
在 C++ 中,您可以使用 lambda 表达式:
exampleFunction([](){ std::cout << "Hello"; });