Another way (How not to write a loop)

Lawrence Crowl crowl at cs.rochester.edu
Wed Feb 17 02:17:59 AEST 1988


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)
]        blah blah blah
]
]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.
]
]Lint will notice this problem, but only with the ANSI compiler
]when it is probably too late.

Fortunately, there is an easy fix.

    while ( index-- > 0 )

-- 
  Lawrence Crowl		716-275-9499	University of Rochester
		      crowl at cs.rochester.edu	Computer Science Department
...!{allegra,decvax,rutgers}!rochester!crowl	Rochester, New York,  14627



More information about the Comp.lang.c mailing list