数组名称不是左值是否有原因? [英] Is there a reason why an array name is not an lvalue?

查看:259
本文介绍了数组名称不是左值是否有原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

int x[10];
int i = 0;
x = &i; //error occurs!

根据 C-参考手册,数组名称不能为左值.因此,x不能为左值.但是,数组名称不能为左值的原因是什么?例如,为什么在第三行出现错误?

According to C - A Reference Manual, an array name cannot be an lvalue. Thus, x cannot be an lvalue. But, what is the reason the array name cannot be an lvalue? For example, why does an error occur in the third line?

推荐答案

您的引用不正确.数组可以是左值(但不能是可修改的左值),而数组名"(标识符)总是 左值.

Your reference is incorrect. An array can be an lvalue (but not a modifiable lvalue), and an "array name" (identifier) is always an lvalue.

以您的示例为例:

int x[10];
int i = 0;
x = &i; //error occurs!

应用C11 6.5.1,第2段:

Apply C11 6.5.1, paragraph 2:

标识符是一个主表达式,前提是它已被声明 作为指定对象(在这种情况下为左值)...

An identifier is a primary expression, provided it has been declared as designating an object (in which case it is an lvalue) ...

我们看到x是一个主要表达式,并且是一个左值,因为它先前已被声明为指定数组对象.

We see that x is a primary expression and is an lvalue, because it has previously been declared as designating an array object.

但是,C语言规则指出,在各种上下文中(包括赋值表达式的左侧)的数组表达式都将转换为指向数组第一个元素的指针,该指针不是左值,即使数组是.具体来说:

However, the C language rules state that an array expression in various contexts, including the left-hand-side of an assignment expression, are converted to a pointer which points at the first element of the array and is not an lvalue, even if the array was. Specifically:

除了它是sizeof运算符,_Alignof运算符或 一元&运算符,或者是用于初始化数组的字符串文字,该表达式具有 类型类型数组"转换为类型为要键入的指针"的表达式 数组对象的初始元素,不是左值.如果数组对象具有 注册存储类,其行为是不确定的.

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

(C11 6.3.2.1第3段).

(C11 6.3.2.1 paragraph 3).

作为上面指定的转换结果的指针不是左值,因为左值指定一个对象,并且没有合适的对象保存该指针值;数组对象保存数组的元素,而不是指向这些元素的指针.

The pointer which is the result of the conversion specified above is not an lvalue because an lvalue designates an object, and there is no suitable object holding the pointer value; the array object holds the elements of the array, not a pointer to those elements.

您在问题中使用的示例意味着您了解数组表达式会衰减(转换为)指针值,但是我认为您无法识别在转换之后,指针值和数组是两个不同的事物.指针不是左值;数组可能是(在您的示例中是).数组是否为左值实际上与您的示例无关;这是您要分配给它的指针值.

The example you use in your question implies that you understand that an array expression decays (is converted to) a pointer value, but I think you are failing to recognize that after the conversion, the pointer value and the array are two different things. The pointer is not an lvalue; the array might be (and in your example, it is). Whether or not arrays are lvalues in fact has no bearing on your example; it is the pointer value that you are trying to assign to.

如果要问:为什么数组在赋值运算符的左侧时会衰减到指针?-那么我怀疑没有特别好的答案.从历史上看,C只是不允许分配给数组.

If you were to ask instead: Why do arrays decay to pointers when they are on the left-hand-side of an assignment operator? - then I suspect that there is no particularly good answer. C just doesn't allow assignment to arrays, historically.

这篇关于数组名称不是左值是否有原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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