问题描述
是什么概念,当我们定义一个无量纲一个全局数组
这显示输出为16。
What is the concept when we define a global array with no dimensionThis shows output as 16.
#include <stdio.h>
#include <stdlib.h>
int arr[];
int main(int argc, char *argv[])
{
arr[1] = 16;
printf("%d\n",arr[1]);
system("PAUSE");
return 0;
}
和连的sizeof(ARR)不起作用。为什么呢?
And even sizeof(arr) doesn't work. Why?
推荐答案
INT ARR [];
是暂定定义的有
6.9.2条第2款说:
Clause 6.9.2, paragraph 2 says:
对于具有文件作用域没有初始化,并且没有存储类说明或存储类说明为静态的对象标识符的声明,构成的暂定定义的。 如果翻译单元包含一个标识符的一个或多个试探性的定义,翻译单元包含该标识符的外部定义,则行为是完全一样的,如果翻译单元包含标识符的文件范围内声明,与复合型作为翻译单元的结束,有一个初始化等于 0
和例2中该条款的第5款明确:
and example 2 in paragraph 5 of that clause clarifies:
如果在包含翻译单元的结束
int i[];
阵列 I
仍具有不完全的类型,隐含的初始化使其有一个元素,它被设置为零的程序启动。
the array i
still has incomplete type, the implicit initializer causes it to have one element, which is set to zero on program startup.
所以的在翻译单元的结束的,你的阵列改编
已键入 INT [1]
。年底前,它的类型是不完整的,因此的sizeof
在不起作用,因为主
,数组类型仍然是不完整。
So at the end of the translation unit, your array arr
has type int[1]
. Before the end, its type is incomplete, hence sizeof
doesn't work, since in main
, the array type is still incomplete.
访问改编[1]
调用不确定的行为,因为改编
只有一个元素。
Accessing arr[1]
invokes undefined behaviour, since arr
has only one element.
这篇关于全球整型数组尺寸不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!