为什么数组作为常量变量的大小不是在C允许的,但允许在C + +? [英] why the size of array as a constant variable is not allowed in C but allowed in C++?

查看:150
本文介绍了为什么数组作为常量变量的大小不是在C允许的,但允许在C + +?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着下面写一个C程序?

I tried to write a c program as below?

const int x = 5;

int main()
{
    int arr[x] = {1, 2, 3, 4, 5};
}

这是给予警告,当我试着用下面GCC编译器。

This is giving warnings when I tried to compile with gcc as below.

simple.c:9:错误:可变大小的物体可能无法初始化

simple.c:9: error: variable-sized object may not be initialized.

但同样被允许在C ++中。当我通过x作为数组大小,为什么X不是恒定处理?

But the same is allowed in C++. When I pass x as array size, why x is not treated as constant?

推荐答案

在C 常量并不意味着不变(即,可评价在编译时)。这仅仅意味着的只读

In C const doesn't mean "constant" (i.e., evaluable at compile time). It merely means read-only.

例如,在一个函数中,这样的:

For example, within a function, this:

const int r = rand();
const time_t now = time(NULL);

是完全有效的。

定义为 const int的对象的名称不是的恒前pression 的。这意味着,(在C之前C99和在C所有版本++)它不能被用来定义一个阵列的长度。

The name of an object defined as const int is not a constant expression. That means that (in C prior to C99, and in all versions of C++) it can't be used to define the length of an array.

尽管C99的(和任选地,C11)支持的可变长度数组的(VLAS),它们不能被初始化。原则上,编译器不知道当它定义的VLA的大小,因此它不能检查一个初始化是否有效。在特定情况下,编译相当可能是能够计算出来,但语言规则旨在覆盖更一般的情况。

Although C99 (and, optionally, C11) support variable-length arrays (VLAs), they can't be initialized. In principle, the compiler doesn't know the size of a VLA when it's defined, so it can't check whether an initializer is valid. In your particular case, the compiler quite probably is able to figure it out, but the language rules are designed to cover the more general case.

C ++是的的相同,但C ++有C不特殊的规则:如果一个对象被定义为常量,它的初始化是一个不断前pression,那么该对象的名称本身是一个恒定的前pression(至少对整型)。

C++ is nearly the same, but C++ has a special rule that C lacks: if an object is defined as const, and its initialization is a constant expression, then the name of the object it itself a constant expression (at least for integral types).

有没有很好的理由认为C已经不采用此功能。在C,如果你想要一个整数类型的名称不变,通常的做法是使用宏:

There's no really good reason that C hasn't adopted this feature. In C, if you want a name constant of an integer type, the usual approach is to use a macro:

 #define LEN 5
 ...
 int arr[LEN] = {1, 2, 3, 4, 5};

请注意,如果您更改 LEN 的价值,你将不得不重新编写初始化。

Note that if you change the value of LEN, you'll have to re-write the initializer.

另一种方法是使用匿名枚举

Another approach is to use an anonymous enum:

 enum { LEN = 5 };
 ...
 int arr[LEN] = {1, 2, 3, 4, 5};

枚举常量的名称实际上是一个不断前pression。在C语言中,由于历史原因,它总是类型的 INT ;在C ++中它是枚举类型。不幸的是,这一招只适用于类型的常量 INT ,因此它限制在范围值从 INT_MIN INT_MAX

The name of an enumeration constant is actually a constant expression. In C, for historical reasons, it's always of type int; in C++ it's of the enumeration type. Unfortunately, this trick only works for constants of type int, so it's restricted to values in the range from INT_MIN to INT_MAX.

这篇关于为什么数组作为常量变量的大小不是在C允许的,但允许在C + +?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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