How to cast C struct just another struct type if their memory size are equal?
Posted
by Eonil
on Stack Overflow
See other posts from Stack Overflow
or by Eonil
Published on 2010-05-02T09:28:48Z
Indexed on
2010/05/02
9:37 UTC
Read the original article
Hit count: 207
I have 2 matrix structs means equal data but have different form like these:
// Matrix type 1.
typedef float Scalar;
typedef struct { Scalar e[4]; } Vector;
typedef struct { Vector e[4]; } Matrix;
// Matrix type 2 (you may know this if you're iPhone developer)
struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;
Their memory size are equal. So I believe there is a way to convert these types without any pointer operations or copy like this:
// Implemented from external lib.
CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz);
Matrix m = (Matrix)CATransform3DMakeScale ( 1, 2, 3 );
Is this possible? Currently compiler prints an "error: conversion to non-scalar type requested" message.
© Stack Overflow or respective owner