Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!hao!husc6!think!ames!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: type-indexed arrays (was: enum - enum ?) Message-ID: <565@haddock.UUCP> Date: Mon, 15-Jun-87 18:27:13 EDT Article-I.D.: haddock.565 Posted: Mon Jun 15 18:27:13 1987 Date-Received: Sun, 21-Jun-87 03:19:17 EDT References: <139@starfire.UUCP> <516@haddock.UUCP> <20540@sun.uucp> <526@haddock.UUCP> <184@auvax.UUCP> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Organization: Interactive Systems, Boston Lines: 42 In article <184@auvax.UUCP> rwa@auvax.UUCP (Ross Alexander) writes: >In article <526@haddock.UUCP>, karl@haddock.UUCP (Karl Heuer) writes: [proposal for a new syntax allowing a type name instead of an array size] > >I can think of only one little quibble with the above: C arrays are >considered to begin with the zero'th element and end at the N-1'th >element, right? So to declare something as > int array[ short ]; >implies that array[] has members indexed by -1, -2, -3, and so on >('short' is a signed type, no?). Agreed. The set of valid subscripts would be -32768 to +32767 (on a vax). >So Karl will also have to think of some way to introduce the declaration of >array index bases, as well as array index ranges, a la Algol-60 or Pascal; What for? The notation "int array[short]" does that implicitly, by (my) definition. The more general case may also be useful, but that can be an independent issue. (And the best solution may turn out to be pascal-like subrange types.) >and this then introduces a rather large can of worms when pointers to based >arrays are passed around. No problem. You can already pass arrays around without knowing the base or size: "int a[10]; p = a+3;" gives you a pointer whose valid subscripts are -3 to +6. >Say I declare an array [named demo, size 21 ints, based at -10]. Now, what >is the value of the token 'demo' ? Is it '&demo[-10]'? Is it '&demo[0]'? If the definition of subscripting is to remain intact, demo must be equal to &demo[0]. This does create some minor problems, but then so does the original idea of an enum-indexed array in a strongly typed world. It may be that this can only be implemented properly by making the array a real datatype. >As a quick out (because I _do_ like the basic idea), how about >saying that subscript types must be unsigned? You get all of the same problems with "enum foo { X=11, Y=12, Z=13 }". Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint