Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get -Wincompatible-pointer-types warnings when casting const double indexed arguments but

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]

Minimal C code test case

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

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.

like image 233
Brian Carcich Avatar asked Mar 01 '18 18:03

Brian Carcich


1 Answers

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.

like image 135
Iharob Al Asimi Avatar answered Nov 15 '22 21:11

Iharob Al Asimi