Regarding output in C++

90 Views Asked by At
int i = 7, j = 3;
int *a = &i, *b = &j;
cout << (*a = *b) << ", " << *(*(&a));

Can someone please explain why is the output 3, 7?

1

There are 1 best solutions below

3
On BEST ANSWER

Your code can be simplified:

int i = 7, j = 3;
cout << (i = j) << ' ' << i;

Here the variable i is accessed and changed in the same statement. Since the order of evaluation of different parts of the same statement is not specified in the C++ standard, compiler may calculate them in any order, and the result may be different on different compilers (or even different versions of the same compiler, or different runs of the same compiler on the same source code, or even different runs of the same compiled program).

Don't write code that changes and accesses something in one statement.