What's wrong with my FOR statements?

06 Feb 2011

Hi, Sorry for such a basic question, but each of my FOR loops, such as:

for (counter; counter < bytecount; counter++) { dataND[XBee_numb][counter] = SerialInput(); chksum_temp = chksum_temp + dataND[XBee_numb][counter]; }

at line #79. The compiler responds: "Expression has no effect" Line: 79.

I have never had problems with FOR statements until trying code in the mbed compiler. This FOR statement was copy-and-paste from working C code. No other errors or problems, just all the FOR loops. Thanks. (All variables are defined, etc...) Jon

06 Feb 2011

for (counter; counter < bytecount; counter++)

for (counter WHAT ; counter < bytecount;

That first part of the for should be something like

for (counter = 0; counter < bytecount; counter++)

The compiler is right, for(counter; has no effect.

07 Feb 2011

Thanks, Andy, but I defined counter = 0; just before the for statement. Do you mean the declaration must occur within the for statement in the mbed compiler? That's something I haven't run into with other compilers. I guess C isn't as standard as I thought. I'll change the for statements tomorrow. Again, thanks.Jon

07 Feb 2011

Hmm. Seems imprudent to not initialize the for loop iterator with the for statement. Should work, but confusing to read next year.

07 Feb 2011

Jon,

Both these are valid:-

counter = 0;
for ( ; counter < bytecount; counter++) {
  // ...
}

// and

for (counter = 0; counter < bytecount; counter++) {
  // ...
}

But, like the compiler says...

counter = 0;
for (counter; counter < bytecount; counter++) {
  // ...
}

the initializer in the for loop "has no effect". Why would you think it should have an effect? It serves no purpose.

07 Feb 2011

Thanks. I appreciate your clarifications. Jon