What am I doing wrong ?

SomeDude lovelydear at mailmetrash.com
Sun Apr 22 14:47:20 PDT 2012


Sorry for the noob questions, but

import std.stdio;

struct Foo {
     int x;
}
void main() {
     auto array = new Foo[10];
     auto i = array.length;
     foreach(Foo f; array) { f.x = --i; write(f.x);}
     writeln();
     foreach(Foo f; array) { write(f.x);}
}

gives me:

PS E:\DigitalMars\dmd2\samples> rdmd bug.d
9876543210
0000000000

Also,

void main() {
     auto array = new Foo[10];
--> for(int i = array.length; i > 1; i--) { array[i].x = i; }
     writeln();
     foreach(Foo f; array) { write(f.x);}
}

throws core.exception.RangeError at bug(8): Range violation on the 
line with the arrow.

What am I doing wrong ?


More information about the Digitalmars-d-learn mailing list