www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Array bounds error

reply David Medlock <amedlock nospam.org> writes:
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
parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
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]
 
 
 -David
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. 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
next sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
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
parent David Medlock <amedlock nospam.org> writes:
Tom S wrote:
 Tom S wrote:
 
 You should either declare the array as:
 //  float A[3][2]
Ooops, it should read: // float A[2][3];
Hmmm Pretty confusing...but se la vi. Thanks, Tom.
May 03 2005
prev sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
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