title: Casting function pointers with different return types
body: <p>This question is about using function pointers, which are not precisely compatible, but which I hope I can use nevertheless as long as my code relies only on the compatible parts. Let's start with some code to get the idea:</p>
  
  <pre><code>typedef void (*funcp)(int* val);
  
  static void myFuncA(int* val) {
    *val *= 2;
    return;
  }
  
  static int myFuncB(int* val) {
    *val *= 2;
    return *val;
  }
  
  int main(void) {
    funcp f = NULL;
    int v = 2;
  
    f = myFuncA;
    f(&amp;v);
    // now v is 4
  
    // explicit cast so the compiler will not complain
    f = (funcp)myFuncB;
    f(&amp;v);
    // now v is 8
  
    return 0;
  }
  </code></pre>
  
  <p>While the arguments of <code>myFuncA</code> and <code>myFuncB</code> are identical and fully compatible, the return values are not and are thus just ignored by the calling code. I <a href="http://coliru.stacked-crooked.com/a/816cf130ea0d6a81" rel="nofollow noreferrer">tried the above code</a> and it works correctly using GCC.</p>
  
  <p>What I learned so far from <a href="https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type">here</a> and <a href="https://stackoverflow.com/questions/31942838/casting-function-pointers">here</a> is that the functions are incompatible by definition of the standard and <strong>may</strong> cause undefined behavior. My intuition, however, tells me that my code example will still work correctly, since it does not rely in any way on the incompatible parts (the return value). However, in the answers to <a href="https://stackoverflow.com/questions/22782166/function-pointers-with-different-return-types">this question</a> a possible corruption of the stack has been mentioned.</p>
  
  <p>So my question is: Is my example above valid C code so it will <strong>always</strong> work as intended (without any side effects), or does it depend on the compiler?</p>
  
  <hr>
  
  <p>EDIT:
  I want to do this in order to use a "more powerful" function with a "les powerful" interface. In my example <code>funcp</code> is the interface but I would like to provide additional functionality like <code>myFuncB</code> for optional use.</p>