When we run this in C++
int main(){
int array[5] = {1,2,3,4,5};
cout << *(&array);
}
Why does it put address of first element of array? I thought that (&array) would give address of first element of array and *(&array) would give VALUE inside it because we are using dereferene * operator on a pointer.
But it shows address of first element of array.
Please can someone tell why it is so?
Thanks
Expected
Value at address in first array index(value of first array element)
>Solution :
There are a number of things happening here that can easily give someone a migraine, so let’s try to look at it from a much simpler approach. Let’s just say that all you have:
int p;
Then, it’s pretty simple to understand: *&p
is the same thing as p
. Taking an address of something, then dereferencing it, brings you right back where you started. You have accomplished absolutely nothing at all, whatsoever.
*(&array);
This is the same exact thing as: *&array
, or array
, or the address of the first value in the array. The End.