www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array construction - why backwards?

reply Barry Denton <basse42 yahoo.com> writes:
The following is from the Tango book

// C-style, or postfix, declarations
int x[3];          // Declares an array of 3 ints
int x[3][5];       // Declares 3 arrays of 5 ints
int (*x[5])[3]; // Declares an array of 5 pointers to arrays of 3 ints
    The preferred syntax is called prefix syntax.
// D-style, or prefix, declarations
int[3] x;          // Declares an array of 3 ints
int[3][5] x;       // Declares 5 arrays of 3 ints
int[3]*[5] x;      // Declares 5 pointers to arrays of 3 ints
    The syntactical differences between the two styles are obvious, but prefix
multidimensional array declarations can be confusing to those with a C
background. You’ll
notice that the order is reversed from the postfix declarations. However,
indexing values
from the multidimensional arrays is done in the same way, no matter how they
are declared,
as in the following example.
int x[5][3];      // Postfix declaration of 5 arrays of 3 ints
int[3][5] y;      // Prefix declaration of 5 arrays of 3 ints
x[0][2] = 1;      // The third element of the first array is set to 1.
y[0][2] = 2;      // the third element of the first array is set to 2.
    As you can see, postfix and prefix come into play only in array
declarations. You index
them both in the same way.


I do not know any other language that does this backwards declaration -postfix.

Can someone tell me why that was needed? It is confusing
Apr 14 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Barry Denton wrote:
 The following is from the Tango book
 
 // C-style, or postfix, declarations
 int x[3];          // Declares an array of 3 ints
 int x[3][5];       // Declares 3 arrays of 5 ints
 int (*x[5])[3]; // Declares an array of 5 pointers to arrays of 3 ints
     The preferred syntax is called prefix syntax.
 // D-style, or prefix, declarations
 int[3] x;          // Declares an array of 3 ints
 int[3][5] x;       // Declares 5 arrays of 3 ints
 int[3]*[5] x;      // Declares 5 pointers to arrays of 3 ints
     The syntactical differences between the two styles are obvious, but prefix
 multidimensional array declarations can be confusing to those with a C
background. You’ll
 notice that the order is reversed from the postfix declarations. However,
indexing values
 from the multidimensional arrays is done in the same way, no matter how they
are declared,
 as in the following example.
 int x[5][3];      // Postfix declaration of 5 arrays of 3 ints
 int[3][5] y;      // Prefix declaration of 5 arrays of 3 ints
 x[0][2] = 1;      // The third element of the first array is set to 1.
 y[0][2] = 2;      // the third element of the first array is set to 2.
     As you can see, postfix and prefix come into play only in array
declarations. You index
 them both in the same way.
 
 
 I do not know any other language that does this backwards declaration -postfix.
 
 Can someone tell me why that was needed? It is confusing

It makes declarations more consistent that way. T[5] is a type that means array of 5 T's. If T is an int that means you have an int[5]. Now what if T is an int[3]? Isn't it logical that to make an array of 5 of those you would do: T[5] ==> (int[3])[5] ==> int[3][5] Unfortunately the designers of C thought that for indexing, the last index should vary the fastest. That means indexing has to be in backwards order from declaration of size, because D tries to be as similar to C syntax as possible. --bb
Apr 14 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fu1g8u$1af6$1 digitalmars.com...
 It makes declarations more consistent that way.

 T[5]  is a type that means array of 5 T's.

 If T is an int that means you have an int[5].

 Now what if T is an int[3]?

 Isn't it logical that to make an array of 5 of those you would do:
 T[5] ==> (int[3])[5] ==> int[3][5]

Exactly. Another reason is when you have mixed "extended" types. This is confusing: int** foo[3][4]; There are decorators on both sides? Huh? This is far more readable: int**[4][3] foo; Since, just like every other declaration, you read right to left: a 3-element array of 4-element arrays of pointers to pointers to ints. Bring function pointers into the mix and.. well you get the idea. Since every "composite" type follows the same right-to-left order, it makes things very easy to grok without having to use some kind of utility to decode types (like there are for C).
Apr 15 2008