January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to albert00 | On Tuesday, 19 January 2016 at 07:21:39 UTC, albert00 wrote:
> Again seems a bit strange "FOR ME" since I declare in one way and access the other way.
>
> albert.
That's because you're stuck in the mindset that 2d arrays are somehow *special*. If I do this:
Row[5] a;
const b = a[4];
Of what type do you expect `b` to be? Of type `Row`, yes?
Now let's define `Row`:
alias Row = int[10];
Row[5] a;
const b = a[9];
const int = c = b[4];
Of what type is `b` now? Of course it is still `Row`.
By substitution, we expect `b[0]` to be equal to `(a[9])[4]`.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On Tuesday, 19 January 2016 at 07:32:22 UTC, tsbockman wrote:
> Now let's define `Row`:
>
> alias Row = int[10];
> Row[5] a;
> const b = a[9];
> const int = c = b[4];
>
> Of what type is `b` now? Of course it is still `Row`.
>
> By substitution, we expect `b[0]` to be equal to `(a[9])[4]`.
Sigh. I should proof-read my stuff more carefully:
alias Row = int[10];
Row[5] a;
const Row b = a[4];
const int = c = b[9];
Of what type is `b` now? Of course it is still `Row`.
By substitution, we expect `b[0]` to be equal to `(a[4])[9]`.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to albert00 | On Tuesday, 19 January 2016 at 07:21:39 UTC, albert00 wrote:
> On Tuesday, 19 January 2016 at 04:50:18 UTC, tsbockman wrote:
>> On Tuesday, 19 January 2016 at 03:20:30 UTC, albert00 wrote:
>>> [...]
>>
>> ... what you're making is an array *of arrays*:
>
> Maybe I was misunderstood, because in fact that is what I was making an array of arrays, but my problem in fact was in accessing it.
>
> Like I said above:
>
> I was declaring int[1][2] arr:
>
> But to access I need to invert the columns like:
>
> arr2[0][0] = 1;
> arr2[1][0] = 2;
>
>
>> int[10][5] a; // An array of 5 (int[10])
>
> Using your example, in my head I'd access the last element as:
>
> a[9][4] but that would give me an error:
>
> Error: array index 9 is out of bounds arr[0 .. 5]
>
> So I need to invert a[4][9].
>
> Again seems a bit strange "FOR ME" since I declare in one way and access the other way.
Try not to think in terms of columns and rows. It makes more sense to think of it in terms of *types*.
Given the following array declaration, which is an array of int:
int[2] nums;
Then any index into nums (such as nums[0]) is going to return an int. The same logic applies given this array declaration, which is an array of int[2]:
int[2][5] numArrays;
Then any index into numArrays is going to return int[2] (for example, numArrays[0]). In order to access the members of the returned array, we need one more index, so that int[0][1] is the second element of the first array. In other words, it's shorthand for:
int[2] arr = numArrays[0];
int num = arr[1];
Seen in this light, it is entirely consistent and not strange at all. It just takes some people a bit of effort to stop thinking in terms of C's multidimensional arrays so that it becomes second-nature to see it in this light. I was using D for quite some time before I finally got used to it.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On Tuesday, 19 January 2016 at 07:35:34 UTC, tsbockman wrote:
> By substitution, we expect `b[0]` to be equal to `(a[4])[9]`.
Apparently I need to get more sleep:
By substitution, we expect `b[9]` to be equal to `(a[4])[9]`.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On Tuesday, 19 January 2016 at 07:32:22 UTC, tsbockman wrote:
>
> That's because you're stuck in the mindset that 2d arrays are somehow *special*. If I do this:
>
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.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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. |
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On Tuesday, 19 January 2016 at 08:27:56 UTC, tsbockman wrote:
> 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.
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.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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...
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On Tuesday, 19 January 2016 at 14:58:51 UTC, tsbockman wrote:
> 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...
In my experience, it's focusing on the types in the D array syntax, rather than the actual ordering, that helps people over the hump. The rows vs. columns bit is useful to demonstrate the difference, but the focus on types clarifies it. That's how I approached it in the section on arrays in Learning D.
|
January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | So guys: Ali, Mike Parker and tsbockman thanks for all your explanation, in fact looking now I and after making some tests I really got it. So: int[2] a1; // Array of 2 elements of type int int[2][5] a2; // Array of 2 elements of type int divided in 5 rows writeln(a2[0]); // = accessing row 0 = [0,0] writeln(a2[4]); // = accessing row 4 = [0,0] If that in mind, now it all makes sense for me, and of course it's consistent as well. Sorry to bother about this, but I think this will help other newcomers. Thanks again for help/tips which helped turn my mindset. Albert. |
Copyright © 1999-2021 by the D Language Foundation