In C++ why can't I write a for() loop like this: for( int i = 1, double i2 = 0;

后端 未结 7 1900
名媛妹妹
名媛妹妹 2020-12-03 20:49

or, \"Declaring multiple variables in a for loop ist verboten\" ?!

My original code was

 for( int i = 1, int i2 = 1; 
      i2 < mid;
      i++, i         


        
相关标签:
7条回答
  • 2020-12-03 21:41

    It's actually a limitation of declaration statements:

    int i=0, j=0, *k=&i;     // legal
    int i=0, double x=0.0;   // illegel
    

    So, basically, the answer to your final question is: (A) & (B) are the same. (C) is different.

    As bta points out:

     z = 1,3,4;
    

    is the same as

     z = 1;
    

    However, that is because = has a higher precedence than ,. If it were written as:

     z = (1,3,4);
    

    then that would be the same as:

     z = 4;
    
    0 讨论(0)
提交回复
热议问题