digitalmars.D.bugs - Array bounds error
- David Medlock (9/9) May 03 2005 void main( char[][] arg )
- Tom S (19/33) May 03 2005 It's not a compiler bug. You're using a D-style array declaration. This
- Tom S (5/7) May 03 2005 Ooops, it should read:
- David Medlock (3/14) May 03 2005 Hmmm Pretty confusing...but se la vi.
- Chris Sauls (23/30) May 03 2005 Tom is basically right, but I think he meant to say "D-style
void main( char[][] arg ) { float[2][3] A; A[0][0] = 1; A[0][1] = 2; A[0][2] = 3; // << error here } test.d(10): array index [2] is outside array bounds [0 .. 2] -David
May 03 2005
David Medlock wrote:void main( char[][] arg ) { float[2][3] A; A[0][0] = 1; A[0][1] = 2; A[0][2] = 3; // << error here } test.d(10): array index [2] is outside array bounds [0 .. 2] -DavidIt's not a compiler bug. You're using a D-style array declaration. This line: // float[2][3] A; says: "array of 3 arrays of 2 floats" D-style declarations read from left ro right. So basically: float[2] is a 2 element array of float float[2][3] is a 3 element array of float[2] On the other hand, A[a][b] reads in the other direction. Actually it's quite logical if you think about it... You should either declare the array as: // float A[3][2] ... or index the array like this: // A[0][0] = 1; // A[1][0] = 2; // A[2][0] = 3; -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
May 03 2005
Tom S wrote:You should either declare the array as: // float A[3][2]Ooops, it should read: // float A[2][3]; -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
May 03 2005
Tom S wrote:Tom S wrote:Hmmm Pretty confusing...but se la vi. Thanks, Tom.You should either declare the array as: // float A[3][2]Ooops, it should read: // float A[2][3];
May 03 2005
Tom S wrote:It's not a compiler bug. You're using a D-style array declaration. This line: // float[2][3] A; says: "array of 3 arrays of 2 floats" D-style declarations read from left ro right.Tom is basically right, but I think he meant to say "D-style declerations read from right to left" rather than the other way. :) It really does simplify things a fair amount, though. To help get your mind around it, consider some declerations: // read: x is: array of: pointer to: int int*[] x; // read: x is: array length 3 of: array of: char char[][3] x; // read: x is: wchar indexed array of: ushort ushort[wchar] x; The only 'special' case in using the function and delegate types: // read: x is: function taking float returning void void function(float) x; The quirk here is that the word function gets "read" before the parameter types... but otherwise the right-to-left thing holds very much true. So as Tom said, you had written this: // read: A is: array length 3 of: array length 2 of: float float[2][3] A; So the moral of the lesson? Declerations of arrays index right-to-left, while expressions on arrays index left-to-right. It seems odd at first, but there aren't any other details to it (that I'm aware of). -- Chris Sauls
May 03 2005