Casting double array to a struct of doubles

前端 未结 6 1756
醉梦人生
醉梦人生 2020-11-30 14:54

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)
{
   do         


        
6条回答
  •  执念已碎
    2020-11-30 15:50

    No, it's not guaranteed.

    The only thing prohibiting any compiler from inserting padding between x and y, or between y and z is common sense. There is no rule in any language standard that would disallow it.

    Even if there is no padding, even if the representation of A is exactly the same as that of double[3], then it's still not valid. The language doesn't allow you to pretend one type is really another type. You're not even allowed to treat an instance of struct A { int i; }; as if it's a struct B { int i; };.

提交回复
热议问题