Portability question

Mike Shannon mikes at 3comvax.UUCP
Thu Nov 7 09:24:24 AEST 1985


Paul Schauble in the cited article 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?
> 
>     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;

	Are you trying to drive us crazy?  Why the !@*? would you ever want
to give something two names??? Isn't life tough enough without this madness??
	I'd bet that your basic problem is something like this:

You have an array of seven structures, one for each day of the week.
You often process these structures as an array, but sometimes, you
want to just deal with one of them.  And you'd like clear, mnemonic names
to add the reader of your code.
	So, how about this:
#define MONDAY 0
#define TUESDAY 1
...
#define SUNDAY 6
struct x a[SUNDAY + 1];

	then you can do:
	for(i = Monday; i <= Sunday; i++) {
		a[i].whatever = something;
	}
	but you can also say:
	a[MONDAY].whatever = something;

	Thus it's readable, but you only have one name for a variable.
-- 
			Michael Shannon {ihnp4,hplabs}!oliveb!3comvax!mikes



More information about the Comp.lang.c mailing list