I would like to have an universal function pointer in C which only specifies the number of arguments, but their size should be of any kind. I thought the following program would crash, but somehow it does work on some devices with 64 Bit architecture but not on 32 Bit. I am not sure if the wideness of architecture is the reason for the different results.
#include <stdio.h>
#include <stdlib.h>
typedef int METHOD(int64_t, int64_t, int64_t, int64_t);
int foo(int i, char c, short s, long l){
printf("i: %d, c: %c s: %hd l:%ld\n", i, c, s, l);
return 5;
}
int main(int argc, char ** argv){
METHOD * ptr=(METHOD*)&foo;
printf("res: %d\n",
ptr(1,'2',3,4));
printf("res: %d\n",
foo(1,'2',3,4));
return 0;
}
Is there a way to make this work on any architecture?
int64_tint64_t(or avoid *) and cast it back when accepting them, but probably there's an easier and more type-safe way to do whatever it is you want.