cast'ing unions

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Aug 12 07:05:45 AEST 1989


In article <2189 at stl.stc.co.uk> dsr at stl.stc.co.uk (David Riches's friend) writes:
>ie float num = (float) u ;

This shows a misunderstanding not only of unions but also of casts.
A cast performs a conversion between two data types, while a union
allows multiple data types to be stored in overlapping locations
(only one type at a time is valid, though; if you store a type into
a union you need to fetch the data as the same type as was stored).

Just use	num = u.f;
or		num = (float) u.i;
depending on which one you really mean.



More information about the Comp.lang.c mailing list