为什么不数组分配在C / C ++? [英] Why are arrays not assignable in C/C++?

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

问题描述

一个可以分配一个结构到另一个,这导致从结构的所有值复制到另一个

One can assign a struct to another, which results in copying all the values from struct to another:

struct
{
    int a, b, c;
} a, b;

...
a = b;

但是,为什么是数组不可转让这样的:

But why are arrays not assignable like that:

int a[3], b[3];
...
a = b;

由于严格来说,都只是结构与可变大小的数组元素,所以这是为什么不允许?这种分配是未使用的反正。当然,它可能看起来像只地址都参与其中,但可以很容易地复制阵列那样(静态)。

Because, strictly speaking, are structs just arrays with variable sized elements, so why is that not allowed? This kind of assignment is unused anyway. Sure, it may seem like only the addresses are involved, but one can easily copy arrays that way ("statically").

推荐答案

回答:

C被​​写入,当阵列前pression评估所述第一元素的地址将被计算的方式。

C is written in such a way that the address of the first element would be computed when the array expression is evaluated.

的是为什么你不能做这样的事情。

This is why you can't do something like

int a[N], b[N];
a = b;


  
  

因为这两个 A B 评估为指针的的在背景;它等同于写 3 = 4 。没有什么记忆,实际上商店的数组中的第一个元素的地址;在翻译阶段的编译器只是计算它 1

because both a and b evaluate to pointer values in that context; it's equivalent to writing 3 = 4. There's nothing in memory that actually stores the address of the first element in the array; the compiler simply computes it during the translation phase1.

<分> 1。重点是我的。

这篇关于为什么不数组分配在C / C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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