Multiple assignment

Ali Çehreli acehreli at yahoo.com
Fri Feb 25 17:05:43 PST 2011


On 02/25/2011 04:56 PM, bearophile wrote:
 > Is this program showing a bug in multiple assignments (DMD 2.052)?
 >
 >
 > void main() {
 >      int i;
 >      int[2] x;
 >      i, x[i] = 1;

I haven't heard about multiple assignments but that's the comma operator 
up there, separating (and sequencing) two expressions:

1) i
2) x[i] = 1

 >      assert(x == [1, 0]); // OK
 >
 >      int j;
 >      int[2] y;
 >      y[j], j = 1;

Again, two expressions:

1) y[j]
2) j = 1

Only the second of both cases have an effect.

Ali

 >      assert(y == [0, 0]); // Not OK
 > }
 >
 >
 > At the end of the program I expect y to be [1,0] instead of [0,0].
 >
 > Yet this C program with GCC:
 >
 > #include "stdio.h"
 > int main() {
 >      int i = 0;
 >      int x[2] = {0, 0};
 >      i, x[i] = 1;
 >      printf("%d %d\n", x[0], x[1]);
 >
 >      int j = 0;
 >      int y[2] = {0, 0};
 >      y[j], j = 1;
 >      printf("%d %d\n", y[0], y[1]);
 >
 >      return 0;
 > }
 >
 >
 > has the same output as DMD:
 > 1 0
 > 0 0
 >
 > Bye,
 > bearophile



More information about the Digitalmars-d-learn mailing list