Doubt - Static multidimension arrays

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 19 06:58:51 PST 2016


On Tuesday, 19 January 2016 at 13:00:19 UTC, Mike Parker wrote:
> Newcomers to D tend to think in terms of C when they declare 
> arrays, so the confusion comes when they find out they have to 
> index it in a way that is the reverse of what they expect. Yes, 
> it's because the declaration syntax is different, but it's 
> always the indexing that trips them up. That's all I meant. It 
> usually isn't obvious what the root of the misunderstanding is 
> until someone explains it.

I guess my problem is that I'm assuming that people who've worked 
with C++ will understand that this: `int arr[5][10]` is *not* an 
"array of arrays"; my point would be clearer if I'd written this:

template<class T, int size> class Array {
     T data[size];
public:
     T& operator[](int x) {
         return data[x]; }
};

int arr2d[5][10];
Array< Array< int, 10 >, 5 > arrOfArrs;

Notice that an actual "array of arrays" declaration has the 
dimensions listed in the same order as D. I'd hope that 
expressing it this way would make it obvious to any experienced 
C/C++ programmer why D's declaration syntax is the way that it is.

Anyway, I'll give it a rest now. I thought this way of looking at 
it would make things easier to understand, but I guess not...


More information about the Digitalmars-d-learn mailing list