Search code examples
cfunctionpointerscastingvoid

How can I cast a void function pointer in C?


Consider:

#include <stdio.h>

int f() {
  return 20;
}

int main() {
    void (*blah)() = f;

    printf("%d\n",*((int *)blah())());  // Error is here! I need help!
    return 0;
}

I want to cast 'blah' back to (int *) so that I can use it as a function to return 20 in the printf statement, but it doesn't seem to work. How can I fix it?


Solution

  • This might fix it:

    printf("%d\n", ((int (*)())blah)() );