为什么我不能创建大小的数组由一个全局变量来确定? [英] Why can't I create an array with size determined by a global variable?

查看:328
本文介绍了为什么我不能创建大小的数组由一个全局变量来确定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么数组 A 没有得到通过全局变量初始化尺寸

Why does the array a not get initialized by global variable size?

#include<stdio.h>

int size = 5;

int main()
{
    int a[size] = {1, 2, 3, 4, 5};
    printf("%d", a[0]);

    return 0;
}

编译错误显示为

可变大小的物体可能无法初始化

variable-sized object may not be initialized

据我来说,数组应该得到由尺寸初始化。

According to me, the array should get initialized by size.

如果我坚持使用全局变量(如果可能的话)?

And what would be the answer if I insist on using global variable (if it is possible)?

推荐答案

在C99,6.7.8 / 3:

In C99, 6.7.8/3:

类型的实体的是
  初始化应的阵列
  未知尺寸或者说是一个对象类型
  没有一个可变长度数组类型。

The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.

6.6 / 2:

一个恒定的前pression进行评估
  在翻译过程中,而不是运行时

A constant expression can be evaluated during translation rather than runtime

6.6 / 6:

这是整型常量前pression
  应具有整数类型,只应
  具有是整数操作数
  常量,枚举常量,
  字符常量的sizeof
  前pressions,其结果是整
  常数,和浮动常量
  是铸件的立即操作数。

An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts.

6.7.5.2/4:

6.7.5.2/4:

如果大小是一个整型常量
  前pression和元素类型有一个
  已知恒定大小,阵列类型是
  不是变长数组类型;
  否则,阵列类型是
  变长数组类型。

If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

A 有变长数组类型,因为尺寸是不是一个整数常量前pression。因此,它不能有一个初始化列表。

a has variable length array type, because size is not an integer constant expression. Thus, it cannot have an initializer list.

在C90,没有沃拉斯,所以code是出于这个原因是非法的。

In C90, there are no VLAs, so the code is illegal for that reason.

在C ++中也有没有沃拉斯,但你可以让尺寸 A const int的。这是因为在C ++中,您可以在内燃机使用 const int的变量。在C你不能。

In C++ there are also no VLAs, but you could make size a const int. That's because in C++ you can use const int variables in ICEs. In C you can't.

presumably你不打算 A 有变长,因此,你需要的是:

Presumably you didn't intend a to have variable length, so what you need is:

#define size 5

如果你实际上并打算 A 有变长的,我想你可以做这样的事情:

If you actually did intend a to have variable length, I suppose you could do something like this:

int a[size];
int initlen = size;
if (initlen > 5) initlen = 5;
memcpy(a, (int[]){1,2,3,4,5}, initlen*sizeof(int));

也许

int a[size];
for (int i = 0; i < size && i < 5; ++i) {
    a[i] = i+1;
}

这很难说,不过,在大小!= 5,它并没有真正意义的指定变长数组大小固定的初始值的情况下什么是应该发生在这里。

It's difficult to say, though, what "should" happen here in the case where size != 5. It doesn't really make sense to specify a fixed-size initial value for a variable-length array.

这篇关于为什么我不能创建大小的数组由一个全局变量来确定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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