On Tuesday, 19 January 2016 at 07:46:59 UTC, Mike Parker wrote:
It's not that he's seeing them as special, it's just that indexing them in D is different than doing so in C or C++. It trips a lot of people up.

No, the difference is actually in C/C++ 's declaration syntax; the way that indexing works is exactly the same as in D.

This C++ code (http://codepad.org/XeVSndBP):

#include <iostream>
#include <cstring>

class Row {
    int data[10];
public:
    int& operator[](int x) {
        return data[x]; }
};

int main(void) {
    int arr2d[5][10];
    Row arrOfArrs[5];

    for(int r = 0; r < 5; ++r)
    {
        for(int c = 0; c < 10; ++c)
        {
            arr2d[r][c]     = (r * 10) + c;
            arrOfArrs[r][c] = (r * 10) + c;
        }
    }

    cout << (arr2d[4][9] == arrOfArrs[4][9]) << endl;
cout << (memcmp(arr2d, arrOfArrs, sizeof(int)*5*10) == 0) << endl;

    return 0;
}


Does exactly the same thing as this D code (http://dpaste.dzfl.pl/1731eb86bc83):

import std.stdio;
import core.stdc.string;

alias Row = int[10];

int main() {
    int[10][5] arr2d;
    Row[5] arrOfArrs;

    for(int r = 0; r < 5; ++r)
    {
        for(int c = 0; c < 10; ++c)
        {
            arr2d[r][c]     = (r * 10) + c;
            arrOfArrs[r][c] = (r * 10) + c;
        }
    }

    writeln(arr2d[4][9] == arrOfArrs[4][9]);
writeln(memcmp(arr2d.ptr, arrOfArrs.ptr, int.sizeof*5*10) == 0);

    return 0;
}

The only relevant difference between the two, is that the order of the row and column specification is swapped in *the declaration*, not when indexing.

Reply via email to