a struct as an multidimensional array index
Ali Çehreli
acehreli at yahoo.com
Fri Jun 10 17:26:48 UTC 2022
On 6/10/22 08:13, z wrote:
> arrays of arrays has different order for declaration and addressing,
> and declaring array of arrays has different order depending on how you
> declare it and wether it's static or dynamic array, *oof*)
>
> To give you an idea of the situation :
> ```D
> int[3][1] a;//one array of 3 int
> writeln(a[0][2]);//first "column", third "row"
> ```
I've written about this multiple times in the past but D's way is
consistent for me. That must be because I always found C's syntax to be
very illogical on this. To me, C's problem starts with putting the
variable name in the middle:
// C code:
int a[1][3]; // Why?
So, first, D moves the variable to its consistent place: after the type:
int i;
int[N] arr;
Both of those are in the form of "type and then name". Good...
And then, here is the consistency with arrays: "type and then square
brackets".
int[] dynamicArray;
int[N] staticArray;
So, here is where you and I differ:
int[3][1] arr; // Ali likes
int[1][3] arr; // z wants
I like it because it is consistently "type and then square brackets".
(It so happens that the type of each element is int[N] in this case.) If
it were the other way, than array syntax would be inconsistent with
itself. :) Or, we would have to accept that it is inside-out like in C.
But of course I understand how it is seen as consistent from C's point
of view. :)
And this is consistent with static vs dynamic as well because again it's
"type and then square brackets":
int[1][] a; // A dynamic array of int[1]
int[][3] b; // A static array of 3 int[]s
Ali
More information about the Digitalmars-d-learn
mailing list