什么`INT常量一个[5]`究竟意味着什么? [英] What does `int const a[5]` really mean?

查看:229
本文介绍了什么`INT常量一个[5]`究竟意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下数组声明:

  INT常量一个[5];

从语言的语义的角度来看,它是完全等同于 const int的一个[5] ?假设是,双方的声明将基本上阅读等的情况下的 a为常数5的整数数组的。

读取第一个声明将是另一种方式的 a是5整数的一个常量数组。

显然,两个发言的逻辑意味着整个阵列是常数;如果一个数组包含5个整数不变,则整个数组是恒定的。或者,如果整个阵列是恒定的,则其所有值也是常数。

据我所知,一个常量数组的概念是有点意义的,因为数组是不可修改的左值(也就是说,他们不能在赋值的左侧出现)。但是,是否有任何情况下,这两个声明会产生不同的行为?

Cdecl.org 拒绝第一个声明为一个语法错误,而目前大多数编译器接受它。)

编辑:

链接的重复询问是否为普通变量常量的顺序事项。随着阵列,这是更混乱了一点,所以我不认为这是一个重复的。


解决方案

  

是完全等同于 const int的一个[5]


是的,是的。


  

读取第一个声明将是一个是5个整数常量数组的另一种方式。


不是真的。你的宣言,书面,适用常量数组元素明确。为了适用常量来阵列本身(而不是它应用到数组元素),你不得不做一些像

  INT(常量一)[5];

但这样的声明是在C语法无效。

这是间接的尝试应用常量到阵列本身可以通过一个中间的typedef进行

 的typedef int类型的[5];
常量A中;

但在这种情况下,每种语言规则,常量预选赛中漏网到数​​组元素,整个事情就是相当于

  const int的一个[5];

请注意,同样,这常量A中的; 上面没有的立即的等同于 const int的一个[5] ; 。这实际上相当于前述 INT(常量一)[5];(!)。 (这是潜行 INT(常量一)[5]一个合法的方式; 过去的编译器的防御功能),但 INT(常量一)[5 ]; 很短住 - 它被立即转换成 const int的一个[5]; 编译器


  

如果一个数组包含5个整数不变,则整个数组是恒定的。或者,如果整个阵列是恒定的,则其所有值也是常数。


嗯,这是不完全正确的。 C语言没有数组对象本身和它的元素进行区分。从概念上讲,这些是不同的实体。例如,当你注意到自己,语言规范说阵列的不可修改的左值的。此,当然,并不prevent数组元素可修改

阵列作为一个整体,单独的数组元素之间的这种概念上的区别,与行为,通过落为常量到底是什么导致了以下不愉快的情况

 的typedef int类型的[5];
A中;
const的A * p =&放大器;一个; //错误!!!

即。它打破了正常的常量,正确性规则,使我们能够初始化 常量T * 指针T * 值。 (C ++故意更新为const,正确性规则作出上述code预期表现,但Ç坚持拒绝它。)

Consider the following array declaration:

int const a[5];

From the semantic standpoint of the language, is it exactly equivalent to const int a[5]? Assuming that is the case, both declarations would essentially read like "a is an array of 5 constant ints".

The alternative way to read the first declaration would be "a is a constant array of 5 ints".

Obviously, both of the statements logically imply that the whole array is constant; if an array consists of 5 constant ints, then the entire array is constant. Alternatively, if the whole array is constant, then all of its values are also constant.

I am aware that the notion of a "constant array" is a bit meaningless since arrays are not modifiable lvalues (that is, they cannot appear on the left side of an assignment). However, are there any circumstances under which these two declarations would yield different behaviour?

(Cdecl.org rejects the first declaration as a syntax error, while most current compilers accept it.)

EDIT:

The linked duplicate asks whether the order of const matters for ordinary variables. With arrays, it is a bit more confusing, so I don't consider this a duplicate.

解决方案

is it exactly equivalent to const int a[5]

Yes, it is.

The alternative way to read the first declaration would be "a is a constant array of 5 ints".

Not really. Your declaration, as written, applies const to array elements specifically. In order to apply the const to the array itself (as opposed to applying it to the array elements), you'd have to do something like

int (const a)[5];

but such declaration is syntactically invalid in C.

An indirect attempt to apply const to the array itself can be made through an intermediate typedef

typedef int A[5];
const A a;

but in this case, per language rules, the const qualifier "falls through" to array elements and the whole thing is just equivalent to

const int a[5];

Note, again, that const A a; above is not immediately equivalent to const int a[5];. It is actually equivalent to the aforementioned int (const a)[5]; (!). (It is a legal way to sneak int (const a)[5]; past compiler's defenses.) But that int (const a)[5]; is very-short lived - it gets immediately transformed into const int a[5]; by the compiler.

if an array consists of 5 constant ints, then the entire array is constant. Alternatively, if the whole array is constant, then all of its values are also constant.

Well, that is not entirely true. C language does differentiate between the array object itself and its elements. Conceptually, these are different entities. For example, as you noted yourself, the language spec says that arrays are non-modifiable lvalues. This, of course, does not prevent array elements to be modifiable.

This conceptual distinction between array as a whole and individual array elements, combined with the "fall through" behavior for const is exactly what leads to the following unpleasant situation

typedef int A[5];
A a;
const A *p = &a; // ERROR!!!

i.e. it breaks the "normal" const-correctness rule that allows us to initialize const T * pointers with T * values. (C++ deliberately updated it const-correctness rules to make the above code to behave "as expected", but C insists on rejecting it.)

这篇关于什么`INT常量一个[5]`究竟意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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