IAR Embedded C编译器对此很满意,我认为它是正确的C代码:
struct incomplete; typedef struct incomplete (*why_not)[2]; struct incomplete {struct incomplete *known_to_work;} array[2]; why_not ok = &array;
但是,gcc和clang对于why_not的定义扼要:
incomplete.c:2:29: error: array type has incomplete element type ‘struct incomplete’ typedef struct incomplete (*why_not)[2]; ^
从技术上讲,没有理由拒绝“指向不完整数组的指针”的定义.毕竟,只有在取消引用这样的变量或执行某些指针算术时才需要结构定义.
我渴望在可能的情况下隐藏结构定义.
C标准对此有何看法?
解决方法
代码使用数组声明符,其中元素类型不完整.根据6.7.6.2/1数组声明符,这是ISO C中的约束违规:
Constraints
In addition to optional type qualifiers and the keyword static,the
[
and]
may delimit an expression or*
. If they delimit an expression (which specifies the size of an array),the expression shall have an integer type. If the expression is a constant expression,it shall have a value greater than zero. The element type shall not be an incomplete or function type.