Search code examples
cprototypefunction-pointersobsoletec23

How to avoid mentioning a function pointer's arguments inside the definition of a function that takes it as argument?


I'm writing this C code that involves passing around a lot of function pointers, and sometimes writing all the arguments that a function pointer takes when defining a function that takes it as an argument can significantly increase the length of the function definition and thus decrease its readability. For instance, say I have this function foo that takes three ints and a function pointer to perform a certain operation. The typical way to declare a function like foo is something along the lines of:

int foo(int a, int b, int c, int (*operation) (int, int, int));

I was wondering if there was a way to avoid the redundancy of having to re-mention the variable types another time in the type of the function pointer.

An answer to this question suggests that it is possible to only use empty parentheses () , which translates in the above example to:

int foo(int a, int b, int c, int (*operation) ());

However, a comment to the same post states that this kind of syntax is going to be removed in C23:

As a side note, this is obsolescent C since year 1989. Empty parenthesis () no prototype style should not be used. And it will finally get removed in upcoming C23


Solution

  • So, just make an alias. I like function aliases over function pointer aliases, so that I know it's a pointer.

    typedef int operation_t(int, int, int);
    int foo(int a, int b, int c, operation_t *operation);