Function pointer with undetermined parameter

c, function-pointers

Solution

I would say use `void (*fp)(void*)`, then pass in pointers to your variables and cast them as void*. It's pretty hacky, but it should work.

eg:

void callIt(void (*fp)(void*))
{
    int x = 5;
    (*fp)((void*)&x);
}

Problem

I want to have a function pointer that can take various types of parameters. How do I do that? The following example (line 1), I want `void (*fp)(int)` to be able to take `void (*fp)(char*)` as well. The following code does not properly compile because I'm passing char* where int is expected so compiling the following code will give you warnings (and won't 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 attempting to pass integer 5 as char* parameter is stupid, but that's not the concern for this question. If you want, you can replace char* with float.

Original source