Is it legal to access pointers to functions with varying argument lists in via a void (*f)() pointer? The program below compiles without warnings with gcc and appears to run correctly, but is it legal C?
#include <stdio.h>
#include <stdlib.h>
typedef void funp();
static void funcall( funp* F, int args, double x)
{
switch( args)
{
case 0: F(); break;
case 1: F(x); break;
}
}
static void fun0( void)
{
printf( "zero\n");
}
static void fun1( double x)
{
printf( "one\t%f\n", x);
}
int main( )
{
funcall( (funp*)fun0, 0, 17.0);
funcall( (funp*)fun1, 1, 17.0);
return EXIT_SUCCESS;
}
I compiled this with
gcc -Wpedantic -Wall -Wextra -std=gnu11 -O2 -o ./funp funp.c
It would be undefined behavior if the nargs parameter didn't match the number of arguments the function took, but is it legal if there is a match?