Array construction - why backwards?

Barry Denton basse42 at yahoo.com
Mon Apr 14 21:24:21 PDT 2008


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


More information about the Digitalmars-d-learn mailing list