Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!world!decwrl!sgi!zok!wattres!steve From: steve@wattres.uucp (Steve Watt) Newsgroups: comp.lang.c Subject: Re: Mixing types in arrays Summary: Use a struct... if not that, then watch for RISC systems. Message-ID: <1991Feb15.053223.813@wattres.uucp> Date: 15 Feb 91 05:32:23 GMT References: <22040@hydra.gatech.EDU> Reply-To: steve@wattres.UUCP (Steve Watt) Organization: Steven Watt, Consultant Lines: 34 In article <22040@hydra.gatech.EDU> cc100aa@prism.gatech.EDU (Ray Spalding) writes: >I keep finding myself wanting to store shorts and longs (as well as >chars) in arrays of char, such as in the following simple example. [ stuff deleted ] >But, are there other pitfalls? Systems on which this won't work? A >better way? One system that this won't work on is the IBM RT (at least under AIX), because a non-aligned integer (read 32 bit) access will get *garbage* in various places. It will produce 'fixed up unaligned access at xxxxxxxxx for pid xxxx' on the DECStation, unless the message is turned off, and will probably blow in strange and subtle ways on other RISC systems. A better way? Use a struct? Probably not as compact, but *much* more obvious. For example, the code (left to the reader) for the following definitions could do (mostly) the same thing, but the search (even linear!) is quite a bit quicker, since it isn't calling strlen all the time. typedef struct { long value; char key[1]; } item; item *data[1000]; Yes, you have to malloc a copy every time, but that's not so bad. But you can easily play all of the various data structure optimization games from here (binary searches with ordered list, etc...) Have fun! -- Steve Watt ...!claris!wattres!steve wattres!steve@claris.com also works Never trust a computer bigger than you can lift.