I'm working on a MASM-to-C transpiler that has zero knowledge of function parameter types. The same generated C code must compile without warnings with:
GCC C17/C23 using
-Wall -Wextra -WpedanticMSVC using
/W4
Among the configurations I tested, the failure occurs with GCC C23:
x86-64 GCC 16.2 -std=c23 -Wall -Wextra -Wpedantic
https://microsoft.godbolt.org/z/vfc96PTM9
#include
int MyFunA(int a){ return a; }
int MyFunB(int a){ return a; }
int (*MyCallBacks[])() = { &MyFunA, &MyFunB };
int main(void)
{
return 0;
}
As noted here, this works cleanly with GCC C17 because () means that the function parameters are unspecified. In C23, () instead means that the function takes no parameters, making int (*)(int) incompatible with int (*)(void).
After trying several alternatives, this solution produces no failure cases:
https://microsoft.godbolt.org/z/3xz9Ebcja
#include
int MyFunA(int a){ return a; }
int MyFunB(int a){ return a; }
void (*MyCallBacks[])(void) = { (void (*)(void))&MyFunA, (void (*)(void))&MyFunB };
int main(void)
{
return 0;
}
Is this the best portable approach for supporting both C17 and C23 while remaining warning/error free with GCC and MSVC?