What am I doing wrong ?
Dmitry Olshansky
dmitry.olsh at gmail.com
Sun Apr 22 14:50:29 PDT 2012
On 23.04.2012 1:47, SomeDude wrote:
> 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);}
Here: Foo f is not a reference but a copy of each array element. Use
foreach( ref f; array)
> }
>
> 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; }
Arrays indices are 0 based, thus last index is array.length-1.
> 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 ?
--
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list