Comma per loop

Why does the next line create errors?

for(int i = 0, int pos = 0, int next_pos = 0; i < 3; i++, pos = next_pos + 1) {
  // …
}

error: expected unqualified-id before β€˜int’
error: β€˜pos’ was not declared in this scope
error: β€˜next_pos’ was not declared in this scope

The compiler is g ++.

+3
source share
2 answers

You can have only one type of declaration for each statement, so you only need one int:

for(int i = 0, pos = 0, next_pos = 0; i < 3; i++, pos = next_pos + 1)
+13
source

In a regular program:

int main()
{

int a=0,int b=0,int c=0;
return 0;    

}

will never work and will not be accepted.

This is what you are actually trying to do inside a for loop!

+3
source

Source: https://habr.com/ru/post/1757581/


All Articles