I believe this is called type punning.
In computer science, type punning is a common term for any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language.
In C and C++, constructs such as type conversion,
union
, andreinterpret_cast
are provided in order to permit many kinds of type punning, although some kinds are not actually supported by the standard language.
As for if it is good practice or not, it makes certain assumptions about the alignment of the data in the structure, but that's rarely an issue. If the alignment of the two structures are different, then severe errors could occur.
If true inheritance is used, as in C++, there is no danger of incompatible alignments, and the efficiency is exactly the same.