Path: utzoo!utgpu!water!watmath!rbutterworth From: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Newsgroups: comp.lang.c Subject: Re: Another way (How not to write a loop) Message-ID: <16982@watmath.waterloo.edu> Date: 17 Feb 88 19:07:27 GMT References: <560@naucse.UUCP> <1988Feb11.200149.25172@sq.uucp> <2115@bsu-cs.UUCP> <6848@sol.ARPA> Organization: U of Waterloo, Ontario Lines: 24 In article <6848@sol.ARPA>, crowl@cs.rochester.edu (Lawrence Crowl) writes: > In article <16941@watmath.waterloo.edu> > rbutterworth@watmath.waterloo.edu (Ray Butterworth) writes: > > I just got hit by yet another way not to write a loop: > > auto size_t index; > > index = sizeof(array) / sizeof(array[0]); > > while (--index >= 0) > > This looks wonderfully portable and works fine on the BSD 4.3 compiler > > (and probably most others). > > But on an ANSI compiler, (size_t) will be an unsigned integer > > and the loop will go forever. > Fortunately, there is an easy fix. > while ( index-- > 0 ) Thanks (sincere, not sarcastic) to those that have sent me this suggestion. I was aware that this is a much better way of doing it. The problem I was trying to point out is that much existing code wasn't written with perfect style, and this is yet another way in which otherwise correct code will break when it is ported to an ANSI compiler (as I found out the hard way).