Why does compiling the minimal test case C code below using gcc5 (5.3.0) generate a [-Wincompatible-pointer-types] warning for the warning() function call that has a const argument and casts that argument using the ConstMyDouble typedef, but not for the nowarning() call that does not use the ConstMyDouble cast, nor for the noconst() call that uses the non-const typedef MyDouble, and how can it be fixed?
There seems to be a subtlety when [const] is used in a typedef and that typedef is used to cast arguments to a function.
The most confusing part is the warning message:
minimal.c:17:7: note: expected ‘const double (*)[2]’ but argument is
of type ‘const ConstMyDouble (*)[2] {aka const double (*)[2]}’
which seems to be saying that const double (*)[2] is not the same as (aka) const double (*)[2]
/* Usage:
*
* ./minimal ; echo ${PIPESTATUS[0]}
* => echo command will output 99 (BASH)
*
* Compile and link, default:
*
* gcc5 minimal.c -o minimal
* => Casts argument to minimal to [ConstMyDouble], a typedef
* => Generates [-Wincompatible-pointer-types] warnings
*
*/
typedef double MyDouble;
typedef const double ConstMyDouble;
int noconst( MyDouble matrix[2][2] ) { return 32; }
int warning( ConstMyDouble matrix[2][2] ) { return 33; }
int nowarning( ConstMyDouble matrix[2][2] ) { return 34; }
int main() {
MyDouble matrix[4];
return noconst(( MyDouble (*)[2])(matrix)) /* No warning */
+ nowarning(( const double (*)[2])(matrix)) /* No warning */
+ warning((ConstMyDouble (*)[2])(matrix)) /* Warning */
I know I can turn off the warnings via the [-Wincompatible-pointer-type] option, but this is a minimal test case and does not represent what I encounter in practice.
In practice these warnings only occur because of similar typedef's and casts in a library I use (https://naif.jpl.nasa.gov/), so I want to know how to fix that to cut down the noise enough to see such warnings from my own code.
I am using GCC 5.3.0; these warnings do not occur with GCC 4.4.7.
This is only a minimal test case; it should not matter that the noconst(), nowarning() and warning() functions do nothing with the argument.
I would think this is related to Strange warning in a C function const multidimensional-array argument, or perhaps others, but here the issue seems to be using a typedef vs. not using a typedef in the cast. Still, if this is a duplicate, I apologize and will remove it.
There appears to be a compiler bug as confirmed by the commenters in your question, in fact @zowl apparently checked that the bug occurs only with GCC 5.3.0 as explained in this comment.
The user @liliscent used the clang compiler and it did not generate a warning either as is explained in this other comment.
So the conclusion that there's a bug in GCC 5.3.0 appears to be valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With