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