Unions

Maarten Litmaath maart at cs.vu.nl
Sat Oct 22 02:32:19 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?

Beside reasons mentioned so-far, there's alignment:

	union	_u {
			int	i;
			char	buf[17];
		} u;

This construct will let you safely read an integer starting at &u.buf[0],
i.e. no bus error will occur.
K&R: a union can be thought of as a struct, all whose fields begin at offset 0.
-- 
Hippic sport:                         |Maarten Litmaath @ Free U Amsterdam:
             a contradiction in terms.|maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.lang.c mailing list