Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!gatech!hubcap!ncrcae!ncr-sd!crash!ford From: ford@crash.UUCP Newsgroups: comp.lang.c Subject: Re: type-indexed arrays (was: enum - enum ?) Message-ID: <1226@crash.CTS.COM> Date: Sat, 13-Jun-87 17:58:29 EDT Article-I.D.: crash.1226 Posted: Sat Jun 13 17:58:29 1987 Date-Received: Sun, 14-Jun-87 05:38:27 EDT References: <139@starfire.UUCP> <516@haddock.UUCP> <20540@sun.uucp> <526@haddock.UUCP> <184@auvax.UUCP> Reply-To: ford@crash.CTS.COM (Michael "Ford" Ditto) Organization: Crash TS, El Cajon, CA Lines: 100 In article <184@auvax.UUCP> rwa@auvax.UUCP (Ross Alexander) writes: >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 No. A supscript 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). In particuar, pointers may be dereferenced with a negative subscript. According to K&R's C Reference Manual, section 14.3: "By definition, the subscript operator [] is interpreted in such a way that E1 [ E2 ] is identical to * ( (E1) + (E2) )." This means that, since negative numbers can be added to pointers, it is possible to reference negatively indexed members of an array. This would only be useful if you had a pointer to the middle of an array, just as it is only useful to use subscripts less than the size of an array. As an example, consider the code to remove a newline from the end of a string: void trim(string) char *string; { while (*string) ++string; if ( string[-1] == '\n' ) string[-1] = '\0'; } This code is optimized for situations where the newline will not usually be present, otherwise the first string[-1] would be *(--string) and the second would be *string. This is because using a non-zero subscript is usually faster than actually decrementing string, but decrementing it is faster than using the non-zero subscript twice. Note that for simplicity, the example does not properly handle the case where a zero-length string is passed. It would reference the byte immediately before the string in memory, which is undefined in general. > [...] Say I declare an array > > int demo[ 21 : -10 ]; > >which I define to mean 'an array of ints, called demo, 21 ints long, >first element is demo[ -10 ]'. This is not a bad idea, but I would prefer something like the pascal syntax: int demo[ -10 .. 10 ]; >Now, what is the value of the token 'demo' ? I think it should be the address of the FIRST element in demo (i.e. demo[-10]). This allows such idiomatic constructs as write(fd, demo, sizeof demo); which would become very non-intuitive if it were necessary to specify the starting or ending indices. > And further, when I then code up >something like 'frobozz( 2, "hi there", demo );' and frobozz() looks >like > > double frobozz( a, b, c ); > int a; > char * b; > int c[]; > { /* and so on... */ > >what is the allowable range of indicies of c[] ? -10 .. +10 ? 0 .. >20 ? > The problem here is that the declaration "int c[]" is misleading, it implies that an array is passed to this function, when only a pointer is passed. It makes sence to declare c as "int *", and then you can pass the address of whichever element of demo[] you like, with "demo" alone refering to the add- ress of the FIRST (not necessarily the zero-th) element. I see no real reason to have negative (or even specifiable) ranges of indices for arrays. This is another "educational" feature of pascal that detracts from C's philosophy of being a 'high-level assembly language', i.e., C is meant to provide a portable representation for operations present in most machines' instruction sets. But negative subscripts to POINTERS can be very useful, almost always has a direct mapping to machine instructions, and is already used in a lot of code that I have seen, including most C compilers' C libraries (in things like strcat, strtok, et al.). Michael "Ford" Ditto -=] Ford [=- P.O. Box 1721 ford@crash.CTS.COM Bonita, CA 92002 ford%oz@prep.mit.ai.edu -- Michael "Ford" Ditto -=] Ford [=- P.O. Box 1721 ford@crash.CTS.COM Bonita, CA 92002 ford%oz@prep.mit.ai.edu