本文介绍了为什么是一个数组变量一样本身的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,如果我们有像 A [10] ,那么 A 和<$ C $数组C>&放大器;一个具有相同的指针值(但不是同一类型)。我想知道为什么为:C这样设计?

对您节省存储&放大器所需的额外空间;一个? ......这是有道理的,当你认为的事实,即 A 不能指向任何其他位置,因此存储&放大器;一个是没有意义的。


解决方案

This isn't a fact, though. If a is an array, a doesn't point anywhere because a is not a pointer. Given int a[42];, a names an array of 42 int objects; it is not a pointer to an array of 42 int objects (that would be int (*a)[42];).

&x gives you the address of the object x; if x is an array type variable, then &x gives you the address of the array; if nothing else, this is consistent with the behavior of & for any other object.

A better question would be "why does an array (like a) decay to a pointer to its initial element in most cases when it is used?" While I don't know with certainty why the language was designed this way, it does make the specification of many things much simpler, notably, arithmetic with an array is effectively the same as arithmetic with a pointer.

这篇关于为什么是一个数组变量一样本身的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:32