Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site hadron.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: Portability question Message-ID: <57@hadron.UUCP> Date: Tue, 5-Nov-85 21:41:39 EST Article-I.D.: hadron.57 Posted: Tue Nov 5 21:41:39 1985 Date-Received: Fri, 8-Nov-85 08:22:25 EST References: <2834@brl-tgr.ARPA> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 41 Keywords: SAY WHAT YOU MEAN! is the whole of the Law. In article <2834@brl-tgr.ARPA> Schauble@MIT-MULTICS.ARPA (Paul Schauble) writes: >Could people please comment on the portability of this structure? If >it's not, why not and how does one set up a structure where one needs to >access the items both by pointer/subscript in a loop and by individual >name in inline code? [ Compressed: ] > struct x {something}; > struct x *ip; > struct { > struct x a; struct x b; struct x c; struct x d; struct x e; > } index; > y = index.a.whatever; z = index.c.whatever; > ip = (struct x *)&index; ...; w = ip[i].whatever; Paul, it's too late for me to flame. But you should realize that just because a struct of structs and an array of structs are packed the same on most machines doesn't mean they always will be! Drop those wonderfully descriptive terms 'a', 'b', etc., and say instead, e.g.: #define NOTHING 0 #define HYDROGEN 1 #define HELIUM 2 #define LITHIUM 3 #define BORON 4 /* I prob'ly missed this one */ #define NELEMENTS 5 struct x index[NELEMENTS]; y = index[NOTHING].whatever; z = index[HELIUM].whatever; ip = &index[0]; /* or ip = index; */ w = ip[i].whatever; This does e x a c t l y what you wanted it to do, and has the advantage that your treatment of everything is uniform. This may (in the long run) show you ways to improve & streamline your code. [Flame deterrant: improve & streamline are obviously not the same, of course.] -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}