How to pass a void function(void) in to another function as parameter in c

2.4k views Asked by At

[enter image description here][1]I have a function void readline() which output a string, and I want to pass it into another function as a parameter, how can I do that,

Thanks for any help.

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

}
1

There are 1 answers

9
Vlad from Moscow On

You can declare the parameter as the function declaration using any name of the parameter. For example

void another_function( void readline( void ) ); 

And the function another_function can be called like

another_function( readline );

The compiler adjusts the function declaration to pointer to the function. So the above declaration is equivalent to

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

EDIT: After you updated your code then the function should be declared like

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

and called like

scorecount( argc, argv, readline );