Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why in C++ for array *(&array) gives array first element address

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading