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 get:
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, unless I'm missing something?
Note: -Wwrite-strings
changes the type of literal strings:
When compiling C, give string constants the type "const char[length]"
For me it's indeed a bug in gcc
5
.
gcc
documentation says
-Wwrite-strings
When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer produces a warning.
So with this declaration:
typeof(*"FOO") x[4];
then &x
is of type const char (*)[4]
when -Wwrite-strings
is present. In Standard C, &x
is of type char (*)[4]
.
This small 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
. So we can see that gcc
5.3
correctly identifies &x
as of type const char (*)[4]
with -Wwrite-strings
.
gcc
should then accept argument &x
when calling a function that has a const char (*)[4]
parameter. Warning for incompatible type is IMHO then a bug in gcc
.
(This bug probably didn't show in previous versions of gcc
simply because gcc
failed (another bug) to identify &x
as a const char (*)[4]
with -Wwrite-strings
in previous gcc
versions. I tested with gcc
4.9.2
and gcc-6-20151206
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With