www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What am I doing wrong ?

reply "SomeDude" <lovelydear mailmetrash.com> writes:
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 bug(8): Range violation on the 
line with the arrow.

What am I doing wrong ?
Apr 22 2012
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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 bug(8): Range violation on the line
 with the arrow.

 What am I doing wrong ?

-- Dmitry Olshansky
Apr 22 2012
prev sibling next sibling parent Mike Wey <mike-wey example.com> writes:
On 04/22/2012 11:47 PM, 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);}

Use ref when you want to modify the original array, without ref you are modifying a copy in the foreach. foreach(ref 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 bug(8): Range violation on the line
 with the arrow.

 What am I doing wrong ?

You set i to the array length of 10 but the array index is zero based so the last item in the array is at index 9. But in the first iteration of the loop you are trying to access the the item at index 10 (array.length) which doesn't exist. -- Mike Wey
Apr 22 2012
prev sibling next sibling parent "SomeDude" <lovelydear mailmetrash.com> writes:
On Sunday, 22 April 2012 at 21:50:32 UTC, Dmitry Olshansky wrote:

Omagad, thank you, too much Java is baaaad for your brains.
Apr 22 2012
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Sun, 22 Apr 2012 23:47:20 +0200
schrieb "SomeDude" <lovelydear mailmetrash.com>:

 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 bug(8): Range violation on the 
 line with the arrow.
 
 What am I doing wrong ?

You could also try: foreach_reverse(i, ref f; array) { f.x = i; } -- Marco
Apr 25 2012