www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Accessing array elements with a pointer-to-array

reply Stephen Tashiro <tashiro zianet.com> writes:
Can the elements of an array be accessed with a pointer using the 
usual indexing notation (e.g."[2][0]") for array elements? - or 
must we treat the elements associated with the pointer as 
1-dimensional list and use pointer arithmetic?

A more elementary question is why array index 2 is out-of-bounds 
in the following
code, which won't compile:

import std.stdio;

     void main()
     {
        ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
        ulong [][] dynamic_array;
        //ulong[][] *pointer;
        ulong *pointer;

        write(static_array);

        dynamic_array = new ulong[][](3,2);

        static_array[2][1] = 6;
        dynamic_array[2][1] = 6;

        pointer = static_array;
        writef("*pointer[2][1] = %d\n", *pointer[2][1]);

        pointer = dynamic_array;
        writef("*pointer[2][1] = %d\n", *pointer[2][1]);

     }
Jan 25
next sibling parent Sergey <kornburn yandex.ru> writes:
On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro 
wrote:
 Can the elements of an array be accessed with a pointer using 
 the usual indexing notation (e.g."[2][0]") for array elements? 
 - or must we treat the elements associated with the pointer as 
 1-dimensional list and use pointer arithmetic?

 A more elementary question is why array index 2 is 
 out-of-bounds in the following
 code, which won't compile:
Be aware of the different dimension size of static and dynamic arrays ```d void main() { import std; ulong [3][2] static_array = [ [0,1,2],[3,4,5] ]; ulong [][] dynamic_array; ulong *pointer; ulong[]* dpointer; dynamic_array = new ulong[][](3,2); static_array[1][1] = 6; dynamic_array[2][1] = 6; writeln(static_array); pointer = static_array.ptr.ptr; writef("*pointer[1][1] = %d\n", *(pointer+4)); writeln(dynamic_array); dpointer = dynamic_array.ptr; writef("*pointer[2][1] = %d\n", dpointer[2][1]); } ```
Jan 25
prev sibling parent reply Kagamin <spam here.lot> writes:
On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro 
wrote:
     void main()
     {
        ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
        static_array[2][1] = 6;
     }
The static array has length 2, so index 2 is out of bounds, must be 0 or 1.
Jan 25
parent reply Stephen Tashiro <tashiro zianet.com> writes:
On Thursday, 25 January 2024 at 20:36:49 UTC, Kagamin wrote:
 On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro 
 wrote:
     void main()
     {
        ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
        static_array[2][1] = 6;
     }
The static array has length 2, so index 2 is out of bounds, must be 0 or 1.
I understand that the index 2 is out of bounds in an array of 2 things. I'm confused about the notation for multidimensional arrays. I thought that the notation uint[m][n] is read from right to left, so it denotes n arrays of m things in each array. So I expected that static_array[k][j] would denotes the kth element of the jth array.
Jan 26
next sibling parent Sergey <kornburn yandex.ru> writes:
On Friday, 26 January 2024 at 11:38:39 UTC, Stephen Tashiro wrote:
 On Thursday, 25 January 2024 at 20:36:49 UTC, Kagamin wrote:
 On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro 
 wrote:
     void main()
     {
        ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
        static_array[2][1] = 6;
     }
The static array has length 2, so index 2 is out of bounds, must be 0 or 1.
I understand that the index 2 is out of bounds in an array of 2 things. I'm confused about the notation for multidimensional arrays. I thought that the notation uint[m][n] is read from right to left, so it denotes n arrays of m things in each array. So I expected that static_array[k][j] would denotes the kth element of the jth array.
Yes, it is a bit tricky Check this nice article if you are interested https://tastyminerals.github.io/tasty-blog/dlang/2020/03/22/multidimensional_arrays_in_d.html
Jan 26
prev sibling next sibling parent Renato <renato athaydes.com> writes:
On Friday, 26 January 2024 at 11:38:39 UTC, Stephen Tashiro wrote:
 On Thursday, 25 January 2024 at 20:36:49 UTC, Kagamin wrote:
 On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro 
 wrote:
     void main()
     {
        ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
        static_array[2][1] = 6;
     }
The static array has length 2, so index 2 is out of bounds, must be 0 or 1.
I understand that the index 2 is out of bounds in an array of 2 things. I'm confused about the notation for multidimensional arrays. I thought that the notation uint[m][n] is read from right to left, so it denotes n arrays of m things in each array. So I expected that static_array[k][j] would denotes the kth element of the jth array.
I think the way it actually works is very intuitive, it goes from inner to outer. If it went the other way around, you couldn't do this: ```d void main() { import std.stdio; alias point = int[2]; point[3] points = [[0, 0], [1, 2], [3, 4]]; writeln(points); } ```
Jan 27
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Friday, 26 January 2024 at 11:38:39 UTC, Stephen Tashiro wrote:
 On Thursday, 25 January 2024 at 20:36:49 UTC, Kagamin wrote:
 On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro 
 wrote:
     void main()
     {
        ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
        static_array[2][1] = 6;
     }
The static array has length 2, so index 2 is out of bounds, must be 0 or 1.
I understand that the index 2 is out of bounds in an array of 2 things. I'm confused about the notation for multidimensional arrays. I thought that the notation uint[m][n] is read from right to left, so it denotes n arrays of m things in each array. So I expected that static_array[k][j] would denotes the kth element of the jth array.
I find the following rule very straightforward to explaining it. If you have an array, it's of type `T[]`. The `T` represents the type of each element. When you access element with index `n` of this array, it's `arr[n]`, which gives you the `n+1`th `T` element in the array. So how do you match this to a static array `ulong[3][2]`? Well, the `T` in this case is `ulong[3]`, and the array part is `[2]`. So this is an array of 2 `ulong[3]`. Therefore, when you index such an array, `static_array[2]` will get the 3rd element of this 2-element array, and fail. -Steve
Jan 27