I want to have a pointer to a function that can accept various types of parameters. How to do it?
In the following example (line 1), I want void (*fp)(int) be able to take void (*fp)(char*) . The following code does not compile properly, because I pass char * where int is assumed, so compiling the following code will give you warnings (and will not work properly).
void callIt(void (*fp)(int)) { (*fp)(5); } void intPrint(int x) { printf("%d\n", x); } void stringPrint(char *s) { printf("%s\n", s); } int main() { void (*fp1)(int) = intPrint; callIt(fp1); void (*fp2)(char*) = stringPrint; callIt(fp2); return 0; }
Note. I know that trying to pass an integer 5 as a char * parameter is stupid, but this is not a problem for this question. If you want, you can replace char * with float.
source share