quote: From Merlin: Somebody mentioned around here not too long ago that since addition is commutative and since array indices are represented as offsets (additions) to the base address of the array, these lines of code are 100% functionally equivalent and valid syntax:
x = array[100]; // becomes x = *(array + 100) x = [100]array; // becomes x = *(100 + array)
Pretty sure it was me, and it''s actually: x = 100[array];
What you have for your second line won''t compile.
-------------------------------------------- Thats the cool thing about programming, if you have a question, just type up and compile. --------------------------------------------
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
The preincrement operator is almost always evaluated before the statement, the only things possibly preceding it being well defined in the Operator precedence chart (++ pre comes before +). Consider: why does a*a + b*b + c*c evaluate as 14 if a = 1, b = 2 and c = 3?
--- Those who can do nothing criticize; those who can, critique.
I just took a peek at my operator precedence chart and noted that post -incrementation had the highest precedence (ie a++). However, pre -incrementation means increasing before evaluating the statement. My bad for botching that explanation.
--- Those who can do nothing criticize; those who can, critique.
Egad! Hmm.. it seems the first preincrementation is evaluated prior to addition, but the other two are evaluated together after the first addition but before the next two.
Ah, compilers! As a matter of fact, you''re right in saying that it has to do with associativity: preincrement is right-to-left while addition is left-to-right, so I think the statement is evaluated as follows:
b = ++a + ++a + (++a); // right-to-left preincrement b = (++a) + (++a + ++a); // left-to-right addition b = c + (d + d); // c = ++a = 2; d = ++c++ (!) = 4 b = 10;
Well, we learn something new everyday. This is one for the lint ads in DDJ!
Ciao!
--- Those who can do nothing criticize; those who can, critique.
ends up with a and b being both 2 because writting the ++ operator after the variable ''a'' means do all the operations needed with ''a'' and THEN add one to a so it becomes something like
int a=1; int b = a(1) + a(1) = 2 THEN ++ happens so a(1)+1 = 2 thats why you must be careful where you put the ++ operator, you get something different doing b=a + ++a