I was trying out different statements to understand how variables and pointers are stored in the memory. I came across this:
void temp(int arr[]){
cout<<&arr<<" "<<&arr+1;
}
int main(){
int arr[] = {1,2,3};
cout<<arr<<" "<<arr+1<<endl;
cout<<&arr<<" "<<&arr+1<<endl;
temp(arr);
return 1;
}
An output is:
0x7fff4d2fe154 0x7fff4d2fe158
0x7fff4d2fe154 0x7fff4d2fe160
0x7fff4d2fe128 0x7fff4d2fe130
I can't understand how
arr
and&arr
print the same address. As per my knowledge,arr
is a pointer to the first element of the array. So shouldn't&arr
print the address of the pointer?Reason for the third output I found online was:
When the array arr is passed to the function it decays to a pointer
What does this mean? Wasn't arr
a pointer in the first place? My understanding is probably flawed, do correct me. Thanks for reading!