Return type qualifiers and function type compatibility (missing diagnostic in GCC)

Andrey Tarasevich atarasevich@comcast.net
Sat Dec 6 08:07:22 GMT 2025


Just noticed that GCC appears to ignore top-level qualifiers on function return 
type when determining type compatibility. 

C's function type compatibility rules (the last paragraph of "6.7.7.4 Function 
declarators") require exact match between function return types, including their 
top-level qualifiers. For parameter types top-level qualifiers are ignored, but 
not for the return type. 

This implies that the function pointer initialization in the following code is 
supposed to be invalid

  const int foo(int a) { return 0; }
  
  int main(void) {
    int (*p)(int) = &foo;
  }

And indeed, Clang reports this with

  error: incompatible function pointer types initializing 'int (*)(int)' with 
    an expression of type 'const int (*)(int)'

Meanwhile, GCC is completely silent about it even in `-pedantic` mode.

Not a big deal, I guess, since top-level qualifiers on function return types are generally considered "pointless", but still, a diagnostic is required for the above example.

-- 
Best regards,
Andrey


More information about the Gcc mailing list