Casting double array to a struct of doubles

前端 未结 6 1755
醉梦人生
醉梦人生 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:51

    I disagree with the consensus here. A struct with three doubles in it, is exactly the same as an array with 3 doubles in it. Unless you specifically pack the struct differently and are on a weird processor that has an odd number of bytes for doubles.

    It's not built into the language, but I would feel safe in doing it. Style wise I wouldn't do it, because it's just confusing.

提交回复
热议问题