5

Assuming that all functions share the same return type, is it valid to call each one by a "generic" function pointer, that is declared with empty parentheses (so it does not specify its arguments)?

Here is an example code, that illustrates it:

#include <stdio.h>

void fun1(void)
{
    printf("fun1\n");   
}

void fun2(int a)
{
    printf("fun2: %d\n", a);
}

void fun3(int a, int b)
{
     printf("fun3: %d %d\n", a, b);
}

int main(void)
{
    void (*pf)(); // pseudo-generic function pointer

    pf = fun1;
    pf();

    pf = fun2;
    pf(0);

    pf = fun3;
    pf(1, 2);

    return 0;
}
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 1
    Possible duplicate of [Function pointer without arguments types?](http://stackoverflow.com/questions/20835534/function-pointer-without-arguments-types) – John Coleman Jun 04 '16 at 16:45
  • Interesting question. Valid as in "does it compile and run as expected?" yes, Does say "compiling with `-Wall` work without warnings?" on my compilers yes :-) – Dilettant Jun 04 '16 at 16:47
  • Yes, no warning even in paranoid mode `-Wall -Wextra -pedantic -Wconversion` by GCC 4.9... @Dilettant – alk Jun 04 '16 at 16:49
  • as per K&R it's legal. – mssirvi Jun 04 '16 at 16:50
  • @JohnColeman: I'd say is very close, but OP's question was more about validity of declaration, than function calling. – Grzegorz Szpetkowski Jun 04 '16 at 16:51
  • I'm not going to spend the time to look it up in the C standard, but empty argument lists are an old K&R feature. In K&R C there was no problem with what you're doing modulo auto-arg promotion. I.e. pointed-to functions can only use arguments of promoted-to types (e.g. float to double). Since the standard tends to honor K&R conventions wherever reasonable, I'd be shocked if this is regarded as incorrect. – Gene Jun 04 '16 at 16:51
  • @JohnColeman: At first I also thought eg. already anwsered at ["Is this a generic function pointer and is it dangerous?"](http://stackoverflow.com/questions/20809790/is-this-a-generic-function-pointer-and-is-it-dangerous) but I think it is a different and interesting (IMO) question also not really matching ["Function pointer without arguments types?"](http://stackoverflow.com/questions/20835534/function-pointer-without-arguments-types). – Dilettant Jun 04 '16 at 16:52
  • 1
    @GrzegorzSzpetkowski I guess that question was more about the legality of the declaration rather than its semantics. I'll retract the close vote. – John Coleman Jun 04 '16 at 16:53
  • Parameters/arguments are optional; that is, a function may contain no parameters. – mssirvi Jun 04 '16 at 16:58

1 Answers1

6

You may do similar stuff, but not exactly that way. When you call a function through a function pointer you have to be sure that the prototype on the calling side is exactly as the function was defined.

Now a function declarations with empty parenthesis isn't even a prototype, so this is not the right way to call it. The main reason is that the calling convention might be slightly different than you'd expect. For () functions special rules apply, namely narrow types are converted to int, unsigned or double.

Now what you may do is store a function pointer in pf as you do, but then you'd always have to convert it back to the correct function pointer before the call, such as

((void (*)(int, int))pf)(a, b);

This is barely readable and much errorprone. You should avoid such gymnastics as much as you can.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177