On Sun, 17 Oct 2021, 23:11 Zoltán Kócsi, <zol...@bendor.com.au> wrote:

> 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?


Please use the gcc-help list for this kind of question. This is off-topic
on this list, see https://gcc.gnu.org/lists.html

Your question is a FAQ:
http://www.c-faq.com/ansi/constmismatch.html



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

Reply via email to