【问题标题】:How to pass a void function(void) in to another function as parameter in c [closed]如何将void函数(void)作为c中的参数传递给另一个函数[关闭]
【发布时间】:2017-09-15 10:22:25
【问题描述】:

[在此处输入图片描述][1]我有一个函数void readline()输出一个字符串,我想将它作为参数传递给另一个函数,我该怎么做,

感谢您的帮助。

int scorecount(argc1, argv1, void readline());
void readline();


int main(int argc, char *argv[]){
    scorecount(argc,argv);
}


int scorecount(argc1, argv1, void readline()){

    output a int
    and I want to use the string from readline function somewhere in
    scorecount

}


void readline(){

    output a string

}

【问题讨论】:

  • 您需要显示更多代码,您的问题在这里还不清楚。或者你可能需要阅读你的 C 教科书。
  • 你是说readline会给你输出字符串,你想把它传递给另一个函数,readline函数的返回类型应该是“string”
  • 也许你想要GNU readline,但它不会返回void
  • 你为什么要链接到实际代码,并输入该代码的一些遥远的近似值?请阅读并在您的问题文本中提供minimal reproducible example

标签: c parameters function-declaration


【解决方案1】:

您可以使用参数的任何名称将参数声明为函数声明。例如

void another_function( void readline( void ) ); 

而函数another_function可以这样调用

another_function( readline );

编译器将函数声明调整为指向函数的指针。所以上面的声明等价于

void another_function( void ( *readline )( void ) ); 

编辑:更新代码后,函数应声明为

int scorecount( int argc, char * argv[], void readline( void ) );
void readline( void );

并像这样称呼

scorecount( argc, argv, readline );

【讨论】:

  • 我是 Stack Overflow 的新手,如何将我的代码放在这里以便大家看到?
  • @HuYunfei 我的回答不清楚还是你还有问题?
  • 我想将readline func传递给一个已经在参数中传递了argc和argv的函数,我应用了你的解决方案,出现错误
  • @HuYunfei 更新你的问题。
  • @HuYunfei 不要引用图片。在您的问题中输入代码。
猜你喜欢
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多