[vox-tech] loop never exits!

Brian Lavender brian at brie.com
Wed Apr 21 20:16:48 PDT 2010


On Wed, Apr 21, 2010 at 07:20:26PM -0700, Matthew Van Gundy wrote:
> On 4/21/10 3:26 PM, Jeff Newmiller wrote:
> >> There are many ways to skin a cat, here's one:
> >>
> >> void reverse(int forward[], int backward[], unsigned int n) {
> >>    unsigned i = n;
> >>    while(i-->  0) {
> >>      backward[n-i-1] = forward[i];
> >>    }
> >> }
> >
> > This reverses and then re-reverses it.
> 
> Nope, just reverses it once.  I'll admit, it isn't an idiomatic 
> construction, but it uses an unsigned index that counts down to reverse 
> an array since that's what Brian seemed to be after.

I just want the equivalent to a reverse iterator. I think using a signed
index value is the way to go. Then, put an invariant condition on the
index, so that it is never negative for accessing an element in the array.

For my program, I am using Gnome's glib's GPtrArray which doens't have an
iterator feature. I didn't expect that it would. I just used a regular
array for my posts not to confuse the problem.

for ( i = NUM_ELEMENTS -1 ; i > 0; i--)
  // access each element

-- 
Brian Lavender
http://www.brie.com/brian/

"For every complex problem there is an answer that is clear, simple, and wrong."
- H. L. Mencken 


More information about the vox-tech mailing list