Unions

KAY kay at hjuxa.UUCP
Fri Oct 21 01:00:50 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 @ 

Probably one of hundreds answering, but....

"Unions provide a way to manipulate different kinds of data in a single area
of storage, without embedding machine-dependent information in the program"
		-K&R, first edition, p138

Actually I usually use them _to_ embed machine _dependant_ information.
I have most often used unions for byte and word swapping when going from one
processor type to another; e.g. PDP to VAX, VAX to 8086, IBM (Serries 1) to
VAX, et cetera:
	union swap {
		short s_val;
		long l_val;
		char bytes[4];	/* see, very machine specific! */
	}

I've also used them to do initial processing on data that always had a
common first element to say what type of data the message contained. Most
recently to put varied information into a constant sized area as in the AT&T
COFF symbol table auxillary (debug) entries. Until you've tried this in
BLISS, you don't appreciate how complex this is.

Got to go.

	Paul S. Kay



More information about the Comp.lang.c mailing list