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

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

问题描述

我试着写下一个c程序如下?

  const int x = 5; 

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

当我试图用gcc编译时, p>

simple.c:9:错误:可变大小的对象可能无法初始化。



在C ++中。当我传递x作为数组大小,为什么x不被视为常量?

解决方案

在C const 不表示常量(即,在编译时可评估)。



例如,在一个函数中,这是:

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

完全有效。



定义为 const int 的对象不是常量表达式。这意味着(在C​​99之前的C和C ++的所有版本中)不能用于定义数组的长度。



虽然C99 ,可选地,C11)支持可变长度数组(VLA),它们不能被初始化。原则上,编译器在定义时不知道VLA的大小,因此无法检查初始化器是否有效。在你的特殊情况下,编译器很可能能够弄清楚,但是语言规则设计为涵盖更一般的情况。



C ++几乎相同,但C ++有一个特殊的规则,C缺乏:如果一个对象定义为 const ,并且它的初始化是一个常量表达式,对象本身是一个常量表达式(至少对于整数类型)。



没有真正好的理由C没有采用这个特性。在C中,如果你想要一个整数类型的名称常量,通常的方法是使用一个宏:

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

请注意,如果您更改 LEN

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

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

枚举常量的名称实际上是一个常量表达式。在C中,由于历史原因,它总是类型 int ;在C ++中是枚举类型。不幸的是,这个技巧只适用于 int 类型的常量,所以它限制在范围从 INT_MIN code> INT_MAX 。


I tried to write a c program as below?

const int x = 5;

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

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

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

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

解决方案

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);

is perfectly valid.

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.

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++ 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).

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};

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};

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天全站免登陆