The type is incompatible with itself in GCC 5 when using -Wwrite-strings

Consider:

char f(const char (*x)[4]); void foo(void) { typeof(*"FOO") x[4]; f(&x); } 

Compile with -Wwrite-strings :

 gcc-5 -c gcc5.c -Wwrite-strings 

You are getting:

 gcc5.c: In function 'foo': gcc5.c:5:7: warning: passing argument 1 of 'f' from incompatible pointer type [-Wincompatible-pointer-types] f(&x); ^ gcc5.c:1:6: note: expected 'const char (*)[4]' but argument is of type 'const char (*)[4]' char f(const char (*x)[4]); ^ 

Looks like a bug in gcc if I'm missing something?

Note: -Wwrite-strings changes the type of literals:

When compiling C, set string constants like "const char [length]"

+6
source share
1 answer

For me, this is really a bug in gcc 5 .

gcc documentation says

-Wwrite-string

When compiling C, give the string constants a type const char [length], so copying the address of one to a non-const char * pointer raises a warning.

So with this declaration:

 typeof(*"FOO") x[4]; 

then &x is of type const char (*)[4] when -Wwrite-strings present. In the standard, C &x is of type char (*)[4] .

This little function:

 void foo(void) { typeof(*"FOO") x[4]; printf("%d\n", __builtin_types_compatible_p(typeof(&x), const char (*)[4])); printf("%d\n", __builtin_types_compatible_p(typeof(&x), char (*)[4])); } 

prints:

 1 0 

with gcc 5.3 and -Wwrite-strings . Thus, we can see that gcc 5.3 correctly identifies &x by type const char (*)[4] with -Wwrite-strings .

gcc must accept the &x argument when calling a function with the const char (*)[4] parameter const char (*)[4] . The incompatible type warning is IMHO, and then an error in gcc .

(This error probably did not appear in previous versions of gcc simply because gcc failed (another error) to identify &x as const char (*)[4] with -Wwrite-strings in previous versions of gcc . I'm with gcc 4.9.2 and gcc-6-20151206 .)

+1
source

All Articles