digitalmars.D.learn - Accessing array elements with a pointer-to-array
- Stephen Tashiro (23/23) Jan 25 Can the elements of an array be accessed with a pointer using the
- Sergey (23/30) Jan 25 Be aware of the different dimension size of static and dynamic
- Kagamin (4/9) Jan 25 The static array has length 2, so index 2 is out of bounds, must
- Stephen Tashiro (7/16) Jan 26 I understand that the index 2 is out of bounds in an array of 2
- Sergey (4/21) Jan 26 Yes, it is a bit tricky
- Renato (12/29) Jan 27 I think the way it actually works is very intuitive, it goes from
- Steven Schveighoffer (12/29) Jan 27 I find the following rule very straightforward to explaining it.
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
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
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
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: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.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 26
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: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.htmlOn Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro wrote: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.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 26
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: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); } ```On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro wrote: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.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 27
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: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. -SteveOn Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro wrote: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.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 27