Why do I get -Wincompatible-pointer-types warnings when casting const with double indices, but

Why compiling the minimum test code below with gcc5 (5.3.0) generates a warning [-Wincompatible-pointer-types] to call the warn () function, which has a const argument and discards this argument using ConstMyDouble typedef, but not for calling nowarning (), which does not use the ConstMyDouble listing, or calling noconst (), which uses the non-constant typedef MyDouble, and how can I fix it?

There seems to be subtlety when [const] is used in a typedef and that typedef is used to pass 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 apparently says that const double (*) [2] does not match (aka) const double (*) [2]

Minimum C Test Code

 /* 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 */ 

Background and assembly

I know that I can turn off warnings using 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 due to a similar typedef and drops in the used library ( https://naif.jpl.nasa.gov/ ), so I want to know how to fix this in order to reduce the noise sufficient 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 just a minimal test case; it doesn't matter that the noconst (), nowarning (), and warning () functions do nothing with the argument.

I would think that this is due to a strange warning in the argument of a multi-dimensional array of const functions const or , but the problem here is to use typedef and not using typedef in translation. However, if this is a duplicate, I apologize and delete it.

+8
c compiler-warnings compilation
source share
1 answer

It seems that the compiler error was confirmed by the commentators in your question, in fact @ zowl apparently checked that the error only occurs with GCC 5.3.0, as it explained in this comment .

User @ liliscent used the clang compiler, and it did not generate warnings, as described in this other comment .

So, the conclusion that the error in GCC 5.3.0 seems to be valid.

+1
source share

All Articles