add/assign problem, part 2.

DCH david@carter-hitchin.clara.co.uk
Mon Jan 1 13:16:00 GMT 2007


>Hi Andrew,

>  > I haven't actually compiled up your program, but immediately alarm
>  > bells are ringing when I see things like
>  >
>  > a = *(vec + i)
>
>It's just a nonstandard way to write vec[i].  I mean, it's weird, but
>it's not actually wrong.  :-)

Sure.  The point I was making is that the variable i was being 
incremented up to a ceiling value
which was determined by a function parameter, i.e. you're trusting 
whoever calls your function
to get the value of this parameter correct.  Much safer to iterate 
with a vector:

for (size_t i = 0, length = vec.size(); i < length; ++i) {
         do something with vec[i];
}

Cheers,
David. 




More information about the Gcc-help mailing list