Using a function prototype in a function without a pointer

My teacher mentioned using a function as a parameter in another function. (I do not mean using pointers. Is this possible? I will show below) I do not understand what he did. Can anyone explain with examples? Thanks to everyone who rated the answers.

using style:

int test(double abc(double)){ // bla bla } 
Function

:

 double abc(double n){ // function main } 

Examples are written by me. I'm not sure they are right.

+4
source share
2 answers

The declaration of this function:

 int test(double abc(double)) { // bla bla } 

is equivalent to:

 int test(double (*abc)(double)) { // bla bla } 

The parameter abc is a parameter of the type of the function pointer ( double (*)(double)) ).

For a standard C reference:

(C99, 6.7.5.3p8) "The declaration of a parameter as a" returning function type "shall be adjusted to" a pointer to the return type of the function ", as in 6.3.2.1."

+4
source

if you use a pointer, you can call the function later in the function test.

 typedef double (*func_type)(double); int test(func_type func) { // bla bla cout << func(3); } // 2 call test(double_func) 

If you want to call a function before calling the test, simply define:

 int test(double) { // bla bla cout << double; } // 2 call test(double_fun(2.0)); 

the right choice depends on your intentions

-1
source

Source: https://habr.com/ru/post/1207845/


All Articles