Strange behavior of array

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 16 04:04:03 PDT 2015


On Friday, October 16, 2015 08:37:09 Mike Parker via Digitalmars-d-learn wrote:
> On Friday, 16 October 2015 at 07:25:16 UTC, Jonathan M Davis
> wrote:
>
> >
> > That does work currently, but there's talk off and on about
> > deprecating the C syntax, so that may happen at some point,
> > just like the C function pointer syntax was deprecated.
> > Regardless, using the C array declaration syntax is generally
> > discouraged - though the fact that the D syntax for static
> > arrays is basically the reverse of what folks expect (much as
> > it makes perfect sense from the compiler's point of view with
> > how types are put together) definitely does make things
> > confusing.
> >
>
> I'm not talking about using C array declaration syntax in D. I'm
> just saying that the indexes in the declaration should be
> reversed. Perhaps I should be more explicit.
>
> int foo[rows][columns];     // In C
> int[columns][rows] foo;     // In D

Oh that. Yeah. In terms of the dimensions,

int[c][b][a] foo;

is equivalent to

auto foo = new int[][][](a, b, c);

though obviously the actual types are different. So, if you use static
arrays, you pretty much have to flip the order of the dimensions and think
of them going from right-to-left in the declaration, whereas everywhere else
(including indexing the static array), they go from left-to-right. It makes
perfect sense given how the compiler reads types, but I'm honestly inclined
to think that it was a mistake, particularly since we already broke the read
outward from the variable name rule from C when we made it so that const had
to go on the left rather than the right and made it use parens. Regardless,
we're stuck with it at this point. And we'll just have to keep explaining to
folks why static arrays aren't working as they expected...

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list