std::array
is ... (quoting from cppreference):
This container is an aggregate type with the same semantics as a struct holding a C-style array
T[N]
as its only non-static data member.
Does that imply that the address of an array is always the same as the address of its first element, i.e. data()
?
#include <array>
#include <iostream>
int main()
{
std::array<int,6> x{};
std::cout << &x << "\n";
std::cout << x.data();
}
Possible output:
0x7ffc86a62860
0x7ffc86a62860
And if yes, is this of any use? Is the following allowed?
int* p = reinterpret_cast<int*>(&x);
for (int i=0;i<6;++i){ std::cout << p[i]; }