【发布时间】:2013-09-15 00:09:08
【问题描述】:
C 标准规定:
如果对象标识符的声明是暂定的 定义并具有内部链接,声明的类型不应是 不完整的类型
“声明的类型不能是不完整的类型”是什么意思?
【问题讨论】:
标签: c definition linkage
C 标准规定:
如果对象标识符的声明是暂定的 定义并具有内部链接,声明的类型不应是 不完整的类型
“声明的类型不能是不完整的类型”是什么意思?
【问题讨论】:
标签: c definition linkage
这意味着你不能拥有:
static int arr[]; // This is illegal as per the quoted standard.
int main(void) {}
数组arr是暂定定义的,类型不完整(缺少有关对象大小的信息),还具有内部链接(static 表示arr 具有内部链接)。
而以下(在文件范围内),
int i; // i is tentatively defined. Valid.
int arr[]; // tentative definition & incomplete type. A further definition
// of arr can appear elsewhere. If not, it's treated like
// int arr[] = {0}; i.e. an array with 1 element.
// Valid.
有效。
【讨论】:
gcc -Wall -pedantic -std=c99 t.c 时,我得到t.c:1:12: error: array size missing in ‘arr’ t.c:1:12: warning: ‘arr’ defined but not used [-Wunused-variable]。 gcc 默认启用许多扩展,可能无法满足 C 标准的所有要求。使用上述选项,它会产生更好的诊断结果。