Can someone explain why "void func_dec(void (*)(int) funcptr);" is illegal

517 views Asked by At

When declaring a functions which takes function pointer as argument as mentioned below the compiler throws error.

 void func_dec(int x, void(*)() funcptr);

Whereas it accepts below declaration,

 void func_dec(int x, void(*funcptr)());

Why the compiler could not recognize the former declaration though it looks logical.

3

There are 3 answers

0
Alexey Frunze On

It is illegal because the formal definition in the language standard says so. As for the reason why it is that way, and it really may seem obscure, here it is:

From The New C Standard: An Economic and Cultural Commentary (v 1.2 from June 24, 2009, section 6.7 Declarations):

The intent of this syntax is for an identifier’s declarator to have the same visual appearance as an instance of that identifier in an expression. For instance, in:

int x[3], *y, z(void);

char (*f(int))[];

the identifier x might appear in the source as an indexed array, y as a dereferenced pointer, and z as a function call. An example of an expression using the result of a call to f is (*f(42))[1].

And the same from The Development of the C Language by Dennis M. Ritchie:

Thus,

int i, *pi, **ppi;

declare an integer, a pointer to an integer, a pointer to a pointer to an integer. The syntax of these declarations reflects the observation that i, *pi, and **ppi all yield an int type when used in an expression. Similarly,

int f(), *f(), (*f)();

declare a function returning an integer, a function returning a pointer to an integer, a pointer to a function returning an integer;

int *api[10], (*pai)[10];

declare an array of pointers to integers, and a pointer to an array of integers. In all these cases the declaration of a variable resembles its usage in an expression whose type is the one named at the head of the declaration.

0
Sourav Ghosh On

This is because,

 void(*)() funcptr

is in invalid syntax on it's own.

Just supply the type while writing the function declaration,

void func_dec(int , void(*) ());

it should be enough. Otherwise, if you want to specify the variable name also, write

 void func_dec(int x, void(*funcptr) ());
1
Klas Lindbäck On

The function parameter name should go in the same place as the function name goes when you declare a function.

Function declaration:

void func();  // correct syntax
void() func;  // incorrrect syntax

Function pointer declaration:

void (*func_ptr)();  // correct syntax
void (*)() func_ptr; // incorrect syntax

Declaring a function pointer becomes easier if you use the following trick:

Take the function declaration. Replace the function name with (*pointerName), or (*) if you want an unnamed version.

Example:

 int func1(char* parm1);        // function
 int (*func1_ptr)(char* parm1); // function pointer

 // function taking a function pointer as parameter:
 void foo(int (*func1_ptr)(char*));
 // The same declaration with an unnamed parameter:
 void foo(int (*)(char*));