Consider the following code segment:
void foo( const char * const m[] );
char *bar( void );
void baz( void )
{
char *m[ 2 ];
m[ 0 ] = bar();
m[ 1 ] = bar();
foo( m );
}
gcc 8.2.0 (and 7.4.1 as well) with -Wall gives a warning, for Intel or
ARM target:
test.c:12:7: warning: passing argument 1 of ‘foo’ from incompatible
pointer type [-Wincompatible-pointer-types]
foo( m );
^
test.c:1:6: note: expected ‘const char * const*’ but argument is of
type ‘char **’
My understanding of the C standard (and I might be mistaken) is that
with the const-s I promised the compiler that foo() won't modify either
the array or the pointed strings, nothing more.
So why is the compiler complaining just because I passed a mutable
array of mutable strings?
Also, how is it different from this case:
void foo( const char *p );
char *bar( void );
void baz( void ) { foo( bar() ); }
which is accepted by the compiler without a warning.
The warning also goes away is m[] is defined as const char *[],
but why is the warning issued in the first place?
Thanks,
Zoltan