Originally Posted by
thescratchy
Well, my book says: i++ copys the existing value to a temporal object, raises the internal value with one and returns that temporal value. But ++i raises the value and returns a reference.
Conceptually, yes. But when used as a standalone expression, a C compiler can be reasonably expected to generate the same code either way. In C++ it may be the case that a compiler will not apply such an optimisation for class types because operator++ could be overloaded such that the two versions are expected to produce different net results even when used standalone.
Originally Posted by
thescratchy
BUT I think this is to easy to be an exam question(university).
Maybe your lecturer thinks otherwise. After all, we can expect that questions will be of varying degrees of difficulty. Besides, you told us what the exam is not about, but did not tell us what the exam is about, i.e., what you are actually being taught and tested.
Originally Posted by
thescratchy
But I cant figure out if I am missing something in the code above
There is the micro-optimisation, which might be implemented by an optimising compiler, that changes this:
Code:
list[i] = list[i] ∗ extra + list[i] ∗ 5;
to:
Code:
list[i] = list[i] ∗ (extra + 5);
which can then be simplified to:
Code:
list[i] *= (extra + 5);
Observe that (extra + 5) is constant in the loop body. Observe also that in the previous line, list[i] is multiplied by another constant, 36. Therefore, the loop can be written as:
Code:
unsigned int i;
extra = 36 * (extra + 5);
for (i = 0; i < size; i++) {
list[i] *= extra;
}