如果是阵列(如int a [4]),那么什么是&放大器;一个? [英] If a is array(such as int a[4];),then what is &a?

查看:164
本文介绍了如果是阵列(如int a [4]),那么什么是&放大器;一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2528318/c-how-come-an-arrays-address-is-equal-to-its-value\">C:为什么一个数组的地址等于它的价值?

我在测试GCC 4.4.1,我觉得&安培; A = A 。我无法理解。我觉得&放大器;一个应该在哪里存储阵列的地址的地址,它不能是相同的。可能有人给我一个很好的解释?非常感谢。

I test in GCC 4.4.1 and I find &a=a. I can't understand it. I think &a should be the address where stores the address of the array, it can't be the same. Could someone give me a good explanation? Thanks very much.

推荐答案

数组是内存中的对象。它有一个地址和大小。这也是事实,在某些情况下,一个数组的衰减的成一个指向它的第一个元素。所以数字,如果这两个 A &放大器;一个作为指针值,比较结果是相等的,相比因为他们都指向存储器的同一地址。但是他们有不同的数据类型: A 的类型 INT [4] (数组4 INT ),或为int * (指针 INT )在某些上下文,而&放大器;一个总是键入 INT(*)[4] (指针数组4 INT )。

An array is an object in memory. It has an address and a size. It's also true that in certain contexts, an array decays into a pointer to its first element. So numerically, if both a and &a are compared as pointer values, they compare as equal, since they both point to the same address in memory. But they have different data types: a has type int[4] ("array 4 of int"), or int* ("pointer to int") in certain contexts, whereas &a always has type int (*)[4] ("pointer to array 4 of int").

 &a points here
 |
 V
+------+------+------+------+
| a[0] | a[1] | a[2] | a[3] |  sizeof(a) == 16
+------+------+------+------+
 ^
 |
 &a[0] also points here
 In certain contexts, 'a' means &a[0]

因此​​,(无效*)A ==(无效*)及。在

另外请注意,因为 A &放大器;一个指向不同的数据类型(特别是,尖-to类型有不同的大小),做指针运算将产生不同的结果。 A + 1 将指向&放大器;一个[1] (一个进步 INT 值),而&安培A + 1 将指向刚刚过去的阵列中的&放到底;一[4 ] ,因为它提出了以一的阵列4 INT 单位:

Also note that because a and &a point to different data types (and in particular, the pointed-to types have different sizes), doing pointer arithmetic will yield different results. a+1 will point to &a[1] (advances by one int value), whereas &a+1 will point to just past the end of the array at &a[4], since it advances by one "array 4 of int" unit:

                       &a+1 points here
                             |
                             V
+------+------+------+------+
| a[0] | a[1] | a[2] | a[3] |
+------+------+------+------+
        ^
        |
  a+1 points here

这篇关于如果是阵列(如int a [4]),那么什么是&放大器;一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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