为什么改编并与放大器;常用3一样的吗? [英] Why is arr and &arr the same?

查看:102
本文介绍了为什么改编并与放大器;常用3一样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程的C / C ++多年,但今天偶然发现让我有点好奇......为什么两个输出产生同样的结果在下面的code? (改编当然是改编[0] ,即指向改编的地址[0] 我本来期望&放大器;改编是该指针的ADRESS,却有着相同的值改编

I have been programming c/c++ for many years, but todays accidental discovery made me somewhat curious... Why does both outputs produce the same result in the code below? (arr is of course the address of arr[0], i.e. a pointer to arr[0]. I would have expected &arr to be the adress of that pointer, but it has the same value as arr)

  int arr[3];
  cout << arr << endl;
  cout << &arr << endl;

注:这个问题是封闭的,但现在它再次打开。 (感谢?)

Remark: This question was closed, but now it is opened again. (Thanks ?)

我知道&放大器;常用3 [0] 改编计算结果为相同的号码,但就是的我的问题!问题是,为什么&放大器;改编改编计算结果为相同的数字。如果改编是文字(不存储anyware),那么编译器应该抱怨,说改编不是一个左值。如果的改编的地址保存起来,然后&放大器;改编应该给我那个位置的地址。 (但不是这种情况)

I know that &arr[0] and arr evaluates to the same number, but that is not my question! The question is why &arr and arr evaluates to the same number. If arr is a literal (not stored anyware), then the compiler should complain and say that arr is not an lvalue. If the address of the arr is stored somewhere then &arr should give me the address of that location. (but this is not the case)

如果我写

const int的* ARR2 =改编;

const int* arr2 = arr;

然后 ARR2 [I] ==改编[I] 任何整数 I ,但&安培;!ARR2 =改编

推荐答案

他们是不一样的。他们只是在同一内存位置。结果
例如。你可以写 ARR + 2 来获取ARR的地址[2],但不是(安培; ARR)+2 做同样的。结果
此外, sizeof的改编的sizeof和放大器; ARR 是不同的。

They're not the same. They just are at the same memory location.
E.G. You can write arr+2 to get the address of arr[2], but not (&arr)+2 to do the same.
Also, sizeof arr and sizeof &arr are different.

这篇关于为什么改编并与放大器;常用3一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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