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

C++11 about *begin(a)

#include<iterator>
#include<iostream>
int a[5][4];
int main()
{
    cout << *begin(a);
}

Why this cout will print the same as begin(a).

It seems the * does not dereferencing the pointer return by begin(a) ?

Can anyone tell me why?

>Solution :

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

Here * affects only the type of the pointer, and not its numerical value.

std::begin(a) returns a pointer to the first element of a, of type int (*)[4] (pointer to an array of 4 ints).

*std::begin(a) returns int[4]. Since cout can’t print arrays directly, the array then decays to the pointer to its first element, of type int *, and is printed as such.

**std::begin(a) would return an int.

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