a struct as an multidimensional array index
Salih Dincer
salihdb at hotmail.com
Sat Jun 11 11:16:21 UTC 2022
On Friday, 10 June 2022 at 17:26:48 UTC, Ali Çehreli wrote:
> 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. [...]
I think so too...
I think D is very consistent with our feelings. That is, the
order in memory is in the form of rows x columns. But yes, in
reverse(column x row) when you set it up statically. This sample
code working on pointers can be a proof:
```d
void main()
{
enum
{
Row = 2,
Column = 3
}
size_t cal = Row * Column * int.sizeof;
auto alloc = new ubyte[cal];
size_t m = Column * int.sizeof;
int[][] aSlice;
foreach (i; 0 .. Row)
{
size_t n = i * m;
aSlice ~= cast(int[])alloc[n .. n + m];
}
auto row = 2;
auto column = 3;
aSlice[row-1][column-1] = 1; // last element
assert(
*( &aSlice[0][0] // first element pointer
+ (row * column - 1)
)
); // no error...
//If you want to see it with your eyes:
import std.stdio;
aSlice.writefln!"%-(%-(%s %)\n%)";
}
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list