Casting double array to a struct of doubles
Is it OK to cast a double array to a struct made of doubles? struct A { double x; double y; double z; }; int main (int argc , char ** argv) { double arr[3] = {1.0,2.0,3.0}; A* a = static_cast<A*>(static_cast<void*>(arr)); std::cout << a->x << " " << a->y << " " << a->z << "\n"; } This prints 1 2 3 . But is it guaranteed to work every time with any compiler? EDIT: According to 9.2.21: A pointer to a standard-layout struct object, suitably converted ? using a reinterpret_cast, points to its initial member (...) and vice versa. if I replace my code with struct A { double & x() { return data[0]; }