Casting one C structure into another

前端 未结 8 702
再見小時候
再見小時候 2020-11-27 03:30

I have two identical (but differently named) C structures:

typedef struct {
      double x;
      double y;
      double z;
} CMAcceleration;


typedef struc         


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 03:46

    That's your only solution (apart from wrapping it into a function):

    vector.x = acceleration.x;
    vector.y = acceleration.y;
    vector.z = acceleration.z;
    

    You could actually cast it, like this (using pointers)

    Vector3d *vector = (Vector3d*) &acceleration;
    

    but this is not in the specs and therefore the behaviour depends on the compiler, runtime and the big green space monster.

提交回复
热议问题