home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c
- Subject: Re: Moving from Pascal to C, Help please!!!!!!
- Date: 1 Jan 1993 01:59:41 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley CA
- Lines: 35
- Message-ID: <28189@dog.ee.lbl.gov>
- References: <78858@hydra.gatech.EDU> <1992Dec31.211801.16730@i88.isc.com>
- NNTP-Posting-Host: 128.3.112.15
-
- In article <1992Dec31.211801.16730@i88.isc.com> katzung@i88.isc.com
- (Brian Katzung) writes:
- >#define ARRAYLOWER 6
- >#define ARRAYUPPER 26
- >int _array[ARRAYUPPER - ARRAYLOWER + 1];
- >#define array (&_array[-ARRAYLOWER])
-
- As the FAQ warns (question 2.11), this technique is not strictly
- kosher. The effect of computing an out-of-bounds address is
- undefined. Most implementations will `do what you mean' here, but
- there are no guarantees.
-
- Note that the trick is fully portable if and only if the zero point
- is contained within the array (i.e., ARRAYLOWER <= 0 < ARRAYUPPER),
- because then &_array[-ARRAYLOWER] is within the array. E.g., if
- the lower bound is -3 and the upper bound is 1, then we have
-
- int _array[1 - (-3) + 1];
-
- or
-
- int _array[5];
-
- and -(-3) is 3, so we compute &_array[3], which is legal (3 < 5).
- (In fact it works when ARRAYUPPER is 0 too, because given an array
- N of T, we can always compute &array[N]; we are just not allowed to
- try to *use* array[N].)
-
- Incidentally, the name `_array' is reserved to the implementation when
- external, so to make this right we also have to use some other name, or
- make `_array' static or automatic. (Technically, _array is safe if
- and only if we give it `internal linkage' or `no linkage'.)
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-