This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Strange gcc specific warning


hi,

On Fri, 24 Jan 2003 05:30:27 +0100 Frank Klemm <pfk@fuchs.offl.uni-jena.de> wrote:

> Hi, 
> 
> gcc is the only compiler which complains about passing R/W arrays to a read
> only function. Can this been removed? 
> Otherwise most people will remove this warning by removing the const from
> the print() function.
> 
> =====================================================================
> /* 
>  *    gcc: 			strange warning, why ???
>  *    g++: 			okay
>  *    all other compilers: 	okay, no warning
>  */
> 
> #include <stdio.h>
> 
> 
> void 
> print ( /* IN */ const float A [2] [16] )
> {
>     int  i;
>     int  j;
>     
>     for ( i = 0; i < 2; i++ ) {
>         for ( j = 0; j < 16; j++ )
>             printf ("%12.6f", A [i] [j] );
>         printf ( "\n" );
>     }
> }
> 
> 
> void 
> invert ( /* IN OUT */ float A [2] [16] )
> {
>     int  i;
>     int  j;
>     
>     for ( i = 0; i < 2; i++ )
>         for ( j = 0; j < 16; j++ )
>             A [i] [j] = -A [i] [j];
> }
> 
> 
> int
> main ( void )
> {
>    float  Array [2] [16];
> 
>    invert ( Array );
>    print  ( Array );
>    
>    return 0;
> }   
> 
> =====================================================================
> 

you can turn off all warnings with the "-w" option. since this is not always 
desirable, there must be a more specific option for this (which i don't know).

anyway, in general, you'd do some cast, but in your case it won't work. 
this is what i mean: 

main ( void )
{
   float  Array [2] [16];

   invert ( Array );
   print  ( (*((const float[][]*) &Array) );
   
   return 0;
}   

in your case, this "trick" won't work, because you're not allowed to cast to
an array type.
i'd suggest, you either remove the const in your print function, or use a
float** instead float[2][16] as function argument. 

please consider that passing a non-const value to a const function is
definitively a type-mismatch, so a warning message is apropriate and sensible.

brg, 
Tolga Dalman.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]