0

I'm not sure why the out put is reversed in second case. Its probably straightforward but I don't think I can spend more time thinking about this. Any help/suggestions are appreciated. Note compiler is VS2012

double array[] = {10.0,20.0,30.0,40.0,50,60.0,70,80,90,100.0};
double *it = array;
int size = 10;
while(i<size) 
{               
    std::cout << "First : " <<  *it;
    it++;
    std::cout << "   Second : " << *it << std::endl; 
    it++;
    i++;        
}
//output
//First : 10.0  Second : 20.0
//First : 30.0  Second : 40.0
//...and so on

/*
while(i<size) 
{
std::cout << std::setprecision(15) << "First : " <<  *it++ << "   Second : " << *it++ <<  std::endl;
i++;
}
//Output is reversed in this case
//First : 20.0  Second : 10.0
//First : 40.0  Second : 30.0
//...and so on

*/
crashmstr
  • 27,457
  • 8
  • 63
  • 78
itachi
  • 192
  • 2
  • 10
  • 1
    Your commented out code is *undefined behavior*. – crashmstr Oct 21 '14 at 18:04
  • possible duplicate of [unexpected output in cout and printf](http://stackoverflow.com/questions/10925530/unexpected-output-in-cout-and-printf) – crashmstr Oct 21 '14 at 18:04
  • 1
    @crashmstr: The uncommented parts are also undefined behavior, reading well past the end of the array. – Mooing Duck Oct 21 '14 at 18:15
  • @MooingDuck true, there are other problems, but I was focused on their specific question of the "ordering" and that it is covered in other questions. – crashmstr Oct 21 '14 at 18:17

1 Answers1

1

This happens because the assignments are made in undefined order, most probably that is the reason why it shows the values reversed. Source: http://en.cppreference.com/w/cpp/language/eval_order

Alexey
  • 144
  • 1
  • 12