什么`INT常量一个[5]`究竟意味着什么? [英] What does `int const a[5]` really mean?
问题描述
考虑以下数组声明:
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数组元素可修改
阵列作为一个整体,单独的数组元素之间的这种概念上的区别,与行为,通过落为
常量
到底是什么导致了以下不愉快的情况组合p>的typedef int类型的[5];
A中;
const的A * p =&放大器;一个; //错误!!!即。它打破了正常的常量,正确性规则,使我们能够初始化
值。 (C ++故意更新为const,正确性规则作出上述code预期表现,但Ç坚持拒绝它。)
常量T *
指针T *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 theconst
to the array itself (as opposed to applying it to the array elements), you'd have to do something likeint (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 typedeftypedef 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 toconst int a[5];
Note, again, that
const A a;
above is not immediately equivalent toconst int a[5];
. It is actually equivalent to the aforementionedint (const a)[5];
(!). (It is a legal way to sneakint (const a)[5];
past compiler's defenses.) But thatint (const a)[5];
is very-short lived - it gets immediately transformed intoconst 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 situationtypedef 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 withT *
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屋!