In the below code-
(Consider this codes is enclosed in the main function with all the necessary headers)
int arr[5] = {10,20,30,40,50};
cout << &(arr);
cout << &(arr+1);
If we just keep the first cout it works and prints the starting address of the array.
But if we keep the second cout it gives compilation error.
Why does it behaves in this manner?
Because
&is taking the address of an lvalue, i.e. an object.arris an lvalue that corresponds to the array. This is why the first works. Butarr+1isn't. It's a temporary result (which by the way corresponds already to an address).If you want to get the address, without compilation error, you can use one of the following:
Here an online demo. By the way, the first can be simplified to
cout<<arr<<endl;