Union type conversions

Michael Zehr tada at athena.mit.edu
Tue Jun 14 01:33:58 AEST 1988


I have question about type conversions and portability.  (please dont' tell
me to buy & read a manual -- i tried and still don't know if this is legal
or not)

I have a function that sometimes needs an int and sometimes needs a float.
(At the function end, it will be able to tell how to treat the arguements.)
The question I have concerns passing the values.  I made a union and 
a function:

typedef union { int integer_value; float float_value} INTEGER_OR_FLOAT;
func(INTEGER_OR_FLOAT a);

What i'm wondering is how to call this function.  should it be:

int i;
float f;
func( (INTEGER_OR_FLOAT) i);
func( (INTEGER_OR_FLOAT) f);

or should it be:

INTEGER_OR_FLOAT temp;
temp.integer_value = i;
func(temp);
temp.float_value = f;
func(temp);


So what it boils down to, is whether casting into a union type is
legal and portable (i don't want to just play with it til it works and
then discover that it only works by accident on my compiler :-).
Thanks in advance for any help.

-michael j zehr



More information about the Comp.lang.c mailing list