【发布时间】:2015-12-23 15:05:07
【问题描述】:
考虑:
char f(const char (*x)[4]);
void foo(void) {
typeof(*"FOO") x[4];
f(&x);
}
用-Wwrite-strings编译:
gcc-5 -c gcc5.c -Wwrite-strings
你得到:
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]);
^
看起来像 gcc 中的一个错误,除非我遗漏了什么?
注意:-Wwrite-strings 改变文字字符串的类型:
编译C时,给字符串常量类型“const char[length]”
【问题讨论】:
-
在C中,
*"FOO"的类型是char。-Wwrite-strings是否应该改变这一点(使编译器非标准)?如果不是,则错误将&x的类型显示为const char (*)[4]:没有const。 -
似乎是一个错误。考虑提交错误报告。
-
@PascalCuoq:即便如此,
char (*)[4]可以转换为const char (*)[4],对吗?为什么会是“不兼容的指针类型”? -
@R_Kapp 不。不可兑换。
const限定符只能通过一次间接转换后添加,因此char (*)[4]可以分配给char(*const)[4],但不能分配给const chr(*)[4]。 -
@R_Kapp 如果我没记错的话,任何稍微复杂一点的规则都可能违反
const的正确性,并且WG14 认为C++ 对隐式const的非常复杂的规则集对于C 来说太复杂了。