Const flagged as incompatible argument
Consider the following code segment: voidfoo( const char * const m[] ); char*bar( void ); voidbaz( 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
gcc-12-20211017 is now available
Snapshot gcc-12-20211017 is now available on https://gcc.gnu.org/pub/gcc/snapshots/12-20211017/ and on various mirrors, see http://gcc.gnu.org/mirrors.html for details. This snapshot has been generated from the GCC 12 git branch with the following options: git://gcc.gnu.org/git/gcc.git branch master revision 5d4d64faa71a6389bfb76bfb3334b63360cf62c0 You'll find: gcc-12-20211017.tar.xz Complete GCC SHA256=3250afd454b73600b61ccf41407bacc225087b9668eeb341408751f6e3384778 SHA1=353ed77e101074ba39aae54c6136c4db5c18233d Diffs from 12-20211010 are available in the diffs/ subdirectory. When a particular snapshot is ready for public consumption the LATEST-12 link is updated and a message is sent to the gcc list. Please do not use a snapshot before it has been announced that way.
Re: Const flagged as incompatible argument
On Sun, 17 Oct 2021, 23:11 Zoltán Kócsi, wrote: > Consider the following code segment: > > voidfoo( const char * const m[] ); > > char*bar( void ); > > voidbaz( 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 >