简体   繁体   English

c - 如何在C中递归调用具有void函数作为参数的函数?

[英]How to recursively call a function that has a void function as a parameter in C?

I am a beginner in C and I wanted to use a void function that has a void function as a passed argument, and then I wanted to call it in a recursive way.我是 C 的初学者,我想使用具有 void 函数作为传递参数的 void 函数,然后我想以递归方式调用它。 For example void inOrder (struct node *root, void (*f) (struct node *i)) But apparently, I can't use it because the argument has no return type.例如void inOrder (struct node *root, void (*f) (struct node *i))但显然,我不能使用它,因为参数没有返回类型。 Does anyone have any suggestions on how to call it recursively?有没有人对如何递归调用它有任何建议? Thank you!谢谢!

The code shown in a comment, void inOrder (root->leftChild, (*f) (root->leftChild)) has two errors:注释中显示的代码void inOrder (root->leftChild, (*f) (root->leftChild))有两个错误:

  • void should not be present. void不应该存在。 When calling a function, we simply write its name followed by the arguments in parentheses.当调用一个函数时,我们只需写下它的名字,后面跟着括号中的参数。 We do not declare its type.我们不声明它的类型。
  • The expression inOrder (root->leftChild, (*f) (root->leftChild)) attempts to call f .表达式inOrder (root->leftChild, (*f) (root->leftChild))试图调用f The second parameter of inOrder is a pointer to a function, so pass a pointer to a function, which is just f : inOrder的第二个参数是一个指向函数的指针,因此传递一个指向函数的指针,即f
inOrder(root->leftChild, f)

Also note that you are not calling f recursively.另请注意,您不是递归调用f f does not call f , directly or indirectly. f不直接或间接调用f inOrder may call itself, and hence it recurses, but it just calls f simply, not recursively. inOrder可能会调用自身,因此它会递归,但它只是简单地调用f ,而不是递归地调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM