Another way (How not to write a loop)

Ray Butterworth rbutterworth at watmath.waterloo.edu
Thu Feb 18 05:07:27 AEST 1988


In article <6848 at sol.ARPA>, crowl at cs.rochester.edu (Lawrence Crowl) writes:
> In article <16941 at watmath.waterloo.edu>
> rbutterworth at 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).



More information about the Comp.lang.c mailing list