home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19102 < prev    next >
Encoding:
Internet Message Format  |  1992-12-31  |  1.8 KB

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