我有两个相同(但名称不同)的 C 结构:
I have two identical (but differently named) C structures:
typedef struct { double x; double y; double z; } CMAcceleration; typedef struct { double x; double y; double z; } Vector3d;现在我想将 CMAcceleration 变量分配给 Vector3d 变量(复制整个结构).我该怎么做?
Now I want to assign a CMAcceleration variable to a Vector3d variable (copying the whole struct). How can I do this?
我尝试了以下操作,但收到这些编译器错误:
I tried the following but get these compiler errors:
vector = acceleration; // "incompatible type" vector = (Vector3d)acceleration; // "conversion to non-scalar type requested"当然我可以单独设置所有成员:
Of course I can resort to set all members individually:
vector.x = acceleration.x; vector.y = acceleration.y; vector.z = acceleration.z;但这似乎很不方便.
最好的解决方案是什么?
What's the best solution?
推荐答案那是你唯一的解决方案(除了将它包装成一个函数):
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.
更多推荐
将一个 C 结构转换为另一个
发布评论