输出一个数组的地址和输出数组第一个元素的地址意思是一样的么?

1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int ar[5] = {1,2,3,4,5};
7 cout << ar << endl;
8 return 0;
9
10 }
~

1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int ar[5] = {1,2,3,4,5};
7 cout << &ar 【0】<< endl;
8 return 0;
9
10 }
~
两个输出结果为什么不一样

数组地址和数组的第一个元素的地址定义确实是不同的:数组的第一个元素的地址叫这个数组的首地址,而这个数组的地址定义为(*)[x]型,不同点是首地址+1是第二个元素的地址,而数组地址+1则跑到这个数组的最后一个元素后面的那个地址;但是数组的第一个元素的地址和这个数组的地址的“值”是相等的,因为都是从同一个地址起算的。但在你提供的两个代码中不存在你说的不同的问题,因为对同一个ar来说,ar就是&ar[0],不会不同,请看代码和结果:

//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
using namespace std;
int main(int argv,char *argc[]){
int ar[5] = {1,2,3,4,5};
cout << ar << endl;
cout << &ar[0] << endl;
return 0;
}

运行结果:

追问

谢谢这么认真的回答😄

温馨提示:答案为网友推荐,仅供参考
相似回答