Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare function pointer parameter

What is the difference between these 2 declarations:

double math_operation(double x, double (*func)(double));
double math_operation(double x, double func(double));

They both seem to work with the same exact call in GCC:

math_operation(2.0, sqrt);

Is it just syntactic sugar or is there more to it?

like image 722
DarkAtom Avatar asked Jun 09 '20 21:06

DarkAtom


People also ask

Can we declare function as pointer?

Function pointers in C need to be declared with an asterisk symbol and function parameters (same as function they will point to) before using them in the program. Declaration of function pointers in C includes return type and data type of different function arguments.

What is the syntax for declaring function pointer?

The following is the syntax for the declaration of a function pointer: int (*FuncPtr) (int,int);

How do you define a function pointer in C++?

The Basic syntax of function pointers We can think of function pointers like normal C++ functions. Where void is the function's return type. *fun_ptr is a pointer to a function that takes one int argument. It's as if we are declaring a function called *fun_ptr which takes int and returns void .


1 Answers

These two function declarations

double math_operation(double x, double (*func)(double));
double math_operation(double x, double func(double));

declare the same one function. You may include the both declarations in your program though the compiler can issue a message that there are redundant declarations.

The compiler implicitly adjusts a parameter having a function type to parameter of pointer type to the function.

On the other hand, a function designator used as an argument is converted to pointer to the function.

[Note: in general all these function declarations declare the same one function

double math_operation( double, double (*)( double ) );
double math_operation( double, double( double ) );
double math_operation( const double, double (*)( double ) );
double math_operation( const double, double( double ) );
double math_operation( double, double (*)( const double ) );
double math_operation( double, double( const double ) );
double math_operation( const double, double (*)( const double ) );
double math_operation( const double, double( const double ) );

Also the pointer to the function itself can have the qualifier const

double math_operation( double, double ( * const )( double ) );
double math_operation( const double, double ( * const )( double ) );
double math_operation( double, double ( * const )( const double ) );
double math_operation( const double, double ( * const )( const double ) );

-end note.]

From the C Standard (6.7.6.3 Function declarators (including prototypes))

8 A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

like image 109
Vlad from Moscow Avatar answered Sep 21 '22 12:09

Vlad from Moscow