Unions

K.LAUX rkl1 at hound.UUCP
Fri Oct 21 02:55:56 AEST 1988


In article <322 at hrc.UUCP>, dan at hrc.UUCP (Dan Troxel VP) writes:
> 
> Except for the memory savings, what are Unions suited for?
> -- 
> Dan Troxel VP of Computer Operations @ 
> Handwriting Research Corporation - 2821 E. Camelback Road Suite 600
> Phoenix, AZ  85016       WK 1-602-957-8870        HM 1-602-435-1240
> UUCP : asuvax!hrc!dan


	The purpose of Unions is so that several different data types can
be stored in a common memory location (but you've already said that).  Of
course you can get into trouble if you have and int and a float in a union
and store a floating value there, then pull it out as an int :-).

	One use of unions is to have different pointer types declared in one and
then having the union as part of a structure.  Then you could use malloc and
cast the pointer returned to various types as needed.  This would allow for your
code to manipulate the structure with multiple types of pointers in it.

--rkl



More information about the Comp.lang.c mailing list