"a[++i] = i" vs "a[i] = ++i"

Ivan Smirnov i.s.smirnov at gmail.com
Fri Dec 20 14:29:54 PST 2013


I was wondering if this behavior is actually documented anywhere? 
Let's say we want to increment i and store the new value at the 
index equal to the new (incremented) value.

	int[int] a;
	int i = 1;
	a[++i] = i;
	writeln(a);
	a[i] = ++i;
	writeln(a);

>> [2:2]
>> [2:2, 3:3]

If any, I would expect it to work for either one of lines but not 
both?

What is interesting, if you compile the same in C (I used clang), 
you get a "warning: unsequenced modification and access to 'i'" 
on both lines and the answer is (as I would initially except)

>> [2:1]
>> [2:1, 3:3]


More information about the Digitalmars-d-learn mailing list