Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pointer to function declaration syntax

What is the difference between the two declarations in case of foo's arguments? The syntax in the second one is familiar to me and declares a pointer to function. Are both declarations fully equivalent?

void foo(int(int));
void foo(int(*)(int));
like image 731
lukeg Avatar asked May 24 '18 00:05

lukeg


People also ask

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 a pointer to a function is declared in C?

Declare a variable x of the integer datatype. Print the value of varisble x. Declare a pointer p of the integer datatype. Define p as the pointer to the address of show() function.

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 * Before function in C?

The * refers to the return type of the function, which is void * .


1 Answers

They are equivalent as long as int(int) and int(*)(int) are used in function parameter lists. In function parameter list the int(int) is automatically adjusted by the language to mean int(*)(int).

It is the same adjustment mechanism that makes int [] parameter declaration equivalent to int * parameter declaration.

Outside of this specific context int(int) and int(*)(int) mean two different things.

like image 124
AnT Avatar answered Sep 23 '22 01:09

AnT