全球整型数组尺寸不 [英] Global integer array with No dimension

查看:84
本文介绍了全球整型数组尺寸不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么概念,当我们定义一个无量纲一个全局数组
这显示输出为16。

What is the concept when we define a global array with no dimension This 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

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

和例2中该条款的第5款明确:

and example 2 in paragraph 5 of that clause clarifies:

如果在包含翻译单元的结束

If at the end of the translation unit containing

   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.

这篇关于全球整型数组尺寸不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆