home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11728 < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.8 KB  |  69 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mdisea!mitchell
  3. From: mitchell@mdd.comm.mot.com (Bill Mitchell)
  4. Subject: Re: Pointers outside of array boundary
  5. Message-ID: <1992Jul29.214010.7093@mdd.comm.mot.com>
  6. Sender: news@mdd.comm.mot.com
  7. Bcc:  mitchell
  8. Reply-To: mitchell@mdd.comm.mot.com (Bill Mitchell)
  9. Organization: Motorola, Mobile Data Division - Seattle, WA
  10. References: <1992Jul28.180945.22332@hubcap.clemson.edu> <mjd.712419017@hal> <1992Jul29.151518.19815@hubcap.clemson.edu>
  11. Date: Wed, 29 Jul 1992 21:40:10 GMT
  12. Lines: 55
  13.  
  14. in comp.lang.c, mjs@hubcap.clemson.edu (M. J. Saltzman) said:
  15.  
  16.  
  17. >In article <mjd.712419017@hal> mjd@hal.gnu.ai.mit.edu (Sievan Janacziewski) writes:
  18. >>>[about 1-based indexing]
  19. >>[...]
  20. >>If 0-based arrays are unclear to your correspondent, then he or she
  21. >>should practice more, so that they become more familiar, rather than
  22. >>resorting to obfuscation to cover his or her inadequacies as a programmer.
  23. >
  24. >While I agree with this sentiment in general, I also have been faced
  25. >with the problem of implementing complicated combinatorial algorithms
  26. >from specifications that describe the algorithms using a 1-based
  27. >indexing scheme.  From a purely aesthetic point of view, re-expressing
  28. >the alogirthms to use 0-based indexing *clearly and correctly* seems
  29. >like a worthwhile goal, but practical considerations (like having to
  30. >get the work done) often take priority, and kluges to fixup the
  31. >results of index calculations often serve to obfuscate the code and
  32. >introduce bugs.  In this case, wasting the 0th element of an array
  33. >seems like a relatively small price to pay, if that's the only way 
  34. >to write 1-based code.
  35. >
  36.  
  37. I agree.  So, apparently, would a couple of programmers named Kernighan
  38. and Ritchie; who are generally regarded as having adequate programming
  39. skills.
  40.  
  41. From k&r-2, pages 111 and 112:
  42.  
  43. static char daytab[2][13] = {
  44.     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  45.     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  46. };
  47. [...]
  48. We started the array daytab with a column of zero so that the month numbers
  49. can run from the natural 1 to 12 instead of 0 to 11.  Since space is not at
  50. a premium here, this is clearer than adjusting the indices.
  51.  
  52. [ end of the quotes]
  53.  
  54. So, there are situations where 1-based arrays make good sense.  In those
  55. situations, 1-based arrays should be used.
  56.  
  57. However, except for those special situations, zero-based arrays should be
  58. used because that is what programmers maintaining the code will expect.
  59.  
  60. The decision between 1-based and 0-based arrays is a judgement call,
  61. and that decision could be made either way depending on factors such
  62. as maintainability considerations, ease of porting a previous implementation
  63. from another language, implementing an algorithm from a published
  64. description, etc.
  65.  
  66. -- 
  67. mitchell@mdd.comm.mot.com (Bill Mitchell)
  68.  
  69.