数组名arr表示数组首元素地址,指向的是数组首元素
&arr指向的是整个数组,二者不同,在进行指针操作,比如+1操作跨过的字节数不同
下面给出例子:
#include<iostream>
#include<cstring>
using namespace std;
void test05(){
int arr[5]={1,2,3,4,5};
cout<<arr<<" "<<&arr<<endl;//0x71fdd0 0x71fdd0
cout<<arr+1<<endl;//0x71fdd4 加了4个字节因为arr指向首元素而不是指向整个数组,指针+1操作是加的跨过的字节数
cout<<&arr+1<<endl;//0x71fde4 和上面不同跨过了16个字节,说明&arr指向的是整个数组
}
int main(){
test05();
return 0;
}
输出结果:
0x71fdd0 0x71fdd0
0x71fdd4
0x71fde4