Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!mailrus!cornell!rochester!rutgers!hjuxa!kay From: kay@hjuxa.UUCP (KAY) Newsgroups: comp.lang.c Subject: Re: Unions Summary: End runs, quite often Keywords: What can they be used for? Message-ID: <1038@hjuxa.UUCP> Date: 20 Oct 88 15:00:50 GMT References: <322@hrc.UUCP> Organization: Digital Equipment Corp. Holmdel, NJ Lines: 30 In article <322@hrc.UUCP>, dan@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