I was trying to use a pointer to iterate through an object array. Printing values one at a time seems to work fine but it's giving the result in a reversed order when I try to print them all together.
#include<iostream>
using namespace std;
class car{
int tires;
public:
car(int a) {tires = a;};
int GetTires(){return tires;};
};
int main() {
car array[4] = { 2, 4, 6, 8 };
car *p;
p = array;
cout << ((p++)->GetTires()) << " " << ((p++)->GetTires()) << " " << ((p++)->GetTires()) << " " << ((p++)->GetTires()) << endl;
//Prints: 8 6 4 2
p = array;
cout << ((p++)->GetTires()) << " ";
cout << ((p++)->GetTires()) << " ";
cout << ((p++)->GetTires()) << " ";
cout << ((p++)->GetTires()) << " " << endl;
//Prints: 2 4 6 8
return 0;
}
Using the C function 'printf' also gives the same output. Can anyone explain why this is happening? TIA.