为什么可修改的左值不能具有数组类型? [英] Why can't a modifiable lvalue have an array type?

查看:38
本文介绍了为什么可修改的左值不能具有数组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C11标准(第6.3.2.1节,左值,数组和函数指示符):

From C11 standard (§6.3.2.1 Lvalues, arrays, and function designators):

一个可修改的左值是一个左值没有数组类型,没有不完整的类型,没有const限定类型,并且如果是结构或联合,则没有任何成员(包括,递归,所有包含的集合或联合的任何成员或元素)都必须具有const限定符类型.

A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

从C入手:

可修改的左值是未声明为const的左值-限定的类型限定符"(第180页),并且没有数组类型.

可修改的左值不能具有数组类型的原因是什么?

What is the reason that a modifiable lvalue can't have an array type?

数组类型的对象是否总是隐式const?

Is an object of an array type always implicitly const?

推荐答案

C语言的设计者认为应该不能通过值分配数组.当时这似乎是一个明智的决定(1970年代初期)-内存和处理器速度非常有限,他们认为让 a = b; 使 a b都引用同一个数组,这比使用 a = b; 复制一个数组的内容到另一个数组要更为普遍.

The designers of the C language decided that it should not be possible to assign arrays by value. At the time this seemed a sensible decision (early 1970s) - memory and processor speed were very limited and they considered that having a = b; make a and b both refer to the same array was something that would be a much more common intent than having a = b; be used to copy the contents of one array to another.

事实上,这已经很普遍了:在B编程语言(C的前身)中,等效于 int a [10]; 的实际含义是分配一个指针和一个块10个整数,并将指针指向10个整数的块.您实际上可以在B中的其他位置创建一个数组点".

In fact this was already common usage: in the B programming language (precursor to C), the equivalent of int a[10]; actually meant to allocate both a pointer, and a block of 10 ints, and point the pointer to the block of 10 ints. You could actually make an array "point" somewhere else in B.

C更改了数组定义的含义,即它仅分配int块;并添加了规则":当您在赋值表达式(和大多数其他表达式)中使用数组名称时,该数组将隐式转换为指向第一个元素的指针.因此,如果 a 是一个指针,而 b 是一个数组,那么您仍然可以编写 a = b; 来使 a 的行为类似于 b 的别名.尽管您不再拥有 a = b; ,其中 a 是一个数组.

C changed the meaning of an array definition that it only allocates the block of ints; and added "The Rule": when you use the array's name in an assignment expression (and most other expressions) the array is implicitly converted to a pointer to the first element. So, if a is a pointer and b is an array, then you can still write a = b; to make a behave like an alias for b. Although you can no longer have a = b; where a is an array.

在1989年的第一个ANSI C标准中,他们增加了按值复制结构的功能(此功能以前在某些编译器中已经存在,但不通用),其推论是,如果结构包含一个数组,则该数组将被复制按价值.但是返回并更改 a = b; 的含义以按值复制数组为时已晚,已经编写了太多依赖于该规则的代码.

In the first ANSI C standard in 1989 they added the ability to copy structs by value (this had existed in some compilers previously but wasn't universal), with the corollary that if the struct contains an array then the array gets copied by value. But it was far too late to go back and change the meaning of a = b; to copy arrays by value, too much code was written that already depends on The Rule.

参考: C语言的发展-Dennis M. Ritchie

这篇关于为什么可修改的左值不能具有数组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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