Portability question

Joseph S. D. Yao jsdy at hadron.UUCP
Wed Nov 6 12:41:39 AEST 1985


In article <2834 at brl-tgr.ARPA> Schauble at 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 at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.lang.c mailing list