Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!mit-eddie!think!ames!ptsfa!ihnp4!alberta!auvax!rwa From: rwa@auvax.UUCP (Ross Alexander) Newsgroups: comp.lang.c Subject: Re: type-indexed arrays (was: enum - enum ?) Message-ID: <187@auvax.UUCP> Date: Sun, 14-Jun-87 19:42:12 EDT Article-I.D.: auvax.187 Posted: Sun Jun 14 19:42:12 1987 Date-Received: Sun, 21-Jun-87 02:11:42 EDT References: <139@starfire.UUCP> <516@haddock.UUCP> <20540@sun.uucp> <1226@crash.CTS.COM> Organization: Athabasca U., Alberta, Canada Lines: 82 Summary: you miss my point, i think In article <1226@crash.CTS.COM>, ford@crash.CTS.COM (Michael Ditto) writes: > In article <184@auvax.UUCP> rwa@auvax.UUCP (Ross Alexander) writes: > > int array[ short ]; > > > >implies that array[] has members indexed by -1, -2, -3, and so on > > No. A subscript must simply be some sort of integral type which the copmiler > can perform multiplication on or stick into an index register (i.e. it must > be something you can add to a pointer). yes, yes, we all know that. float and double subscript values are strictly for BASIC, right :-) ?? what i was driving at was "how much storage is the compiler going to ALLOCATE for array[ short ] ?", and the corollory question is "what is the value of the index of the first and last allocated elements of array[]?". by analogy with Pascal, one might expect the first allocated element to have a negative index value, since 'short' is a signed type, and is really "MIN_SHORT_VALUE .. MAX_SHORT_VALUE" when you think of it as a subrange type (subrange of int). on 'typical' architectures, this might be -0x8000 .. 0x7fff or whatever. this was my meaning (poorly expressed, i grant you) when i said 'indexed by -1, -2, -3, and so on'. at any rate, the array will have (some) elements indexed by negative values, and (some) by non-negative values. now, getting back to the can of worms (smile, gentle people), say i declare int char_count[ char ]; (I want to count character frequencies in some input, or something), and further let us say that chars are signed, so this declaration is equivalent to int char_count[ MIN_CHAR .. MAX_CHAR ]; with MIN_CHAR == -128, and MAX_CHAR == 127. (As an aside, this is precisely the case locally). now, you suggest that the token 'char_count' would have the value '& char_count[ MIN_CHAR ]'. (actually, it's a pointer constant, but never mind). then i can say that '* ( char_count + 3 )' references an int three int's further on in memory than '& char_count[ MIN_CHAR ]'. ok? and of course, 'char_count[ 3 ]' is the third int after 'char_count[ 0 ]', which to my way of thinking is in the middle of the array. but we get into a small problem when we observe that '* ( char_count + 3 )' and 'char_count[ 3 ]' are formally equivalent, but talk about entirely different places (whoops!!). this _reductio ad absurdem_ leads me to think that fooling with the current semantics of the pointer constant 'char_count' is not a good idea, i.e., it should still mean '& char_count[ 0 ]' regardless of the fact that happens to be the middle of the array. And you yourself observed how much grief that is going to cause. has anybody got a bright idea? i am (reluctantly) forced to think that fiddling with the semantics of C array declarations is not wise. as any aside, any one have a good way to introduce sets into C? we already have enum's, i really would like to have sets and set operations. i know that you can fake it for small sets ( <= number of bits in a 'long' ) very easily, but it gets ugly after that. and writing enum days { sunday, monday, /* ... */, friday, saturday }; long days_off; int x; days_off = ( 1 << sunday ) | ( 1 << saturday ); if ( ( days_off & ( 1 << x ) ) != 0 ) { /* do something */ }; is a little less intuitive than ( i create syntax freely here, the semantics are pretty much the same ) set days_off[ days ]; days_off = [ sunday, saturday ]; if ( days_off[ x ] ) { /* do something */ }; i don't like the notation especially, but... ...!ihnp4!alberta!auvax!rwa Ross Alexander, Athabasca University