Advertisement

#define Macros... need some help

Started by May 07, 2001 11:46 PM
19 comments, last by darkpunk 23 years, 8 months ago
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.
--------------------------------------------

Not always, for example, what does a = i++; do?

You can compile it and see how your compiler handles it, but the statement is undefined, see 11.33 What''s the difference between implementation-defined, unspecified, and undefined behavior?.


-----------------------------

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
-----------------------------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
Advertisement
Yes, you''re right; my bad.
Oops...

Meant:

int a = 1;
int b = ++a + ++a + ++a;

b = ?

G''luck,
-Alamar
b = 12

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.
Advertisement
Ok, now actually plug it into a compiler and see what you get : )

It''s about right to left precedence : )

G''luck,
-Alamar
10!

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.
If you do:
  int a = 1;int b = a + a++;  

why are a and b both 2?
Simple,

int a=1;
int b=a + a++;

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

This topic is closed to new replies.

Advertisement