考虑:
char f(const char (*x)[4]);
void foo(void) {
typeof(*"FOO") x[4];
f(&x);
}
Run Code Online (Sandbox Code Playgroud)
编译-Wwrite-strings
:
gcc-5 -c gcc5.c -Wwrite-strings
Run Code Online (Sandbox Code Playgroud)
你得到:
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]);
^
Run Code Online (Sandbox Code Playgroud)
看起来像gcc中的bug,除非我遗漏了什么?
注意:-Wwrite-strings
更改文字字符串的类型:
编译C时,给字符串常量类型"const char [length]"
对我来说这确实是一个错误gcc
5
。
gcc
文件说
-Wwrite-字符串
编译 C 时,将字符串常量指定为 const char[length] 类型,以便将字符串常量的地址复制到非常量 char * 指针中会产生警告。
因此,有了这个声明:
typeof(*"FOO") x[4];
Run Code Online (Sandbox Code Playgroud)
then是when存在的&x
类型。在标准 C 中,类型为。const char (*)[4]
-Wwrite-strings
&x
char (*)[4]
这个小功能:
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]));
}
Run Code Online (Sandbox Code Playgroud)
印刷:
1
0
Run Code Online (Sandbox Code Playgroud)
与gcc
5.3
和-Wwrite-strings
。所以我们可以看到gcc
5.3
正确地识别为&x
类型const char (*)[4]
-Wwrite-strings
。
gcc
然后&x
在调用具有参数的函数时应该接受参数const char (*)[4]
。恕我直言,对不兼容类型的警告是一个错误gcc
.
(这个错误可能没有在以前的版本中显示gcc
,只是因为在以前的版本中gcc
未能(另一个错误)识别&x
为const char (*)[4]
with 。我用和进行了测试。)-Wwrite-strings
gcc
gcc
4.9.2
gcc-6-20151206