Accessing array elements with a pointer-to-array

Stephen Tashiro tashiro at zianet.com
Thu Jan 25 20:11:05 UTC 2024


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]);

     }



More information about the Digitalmars-d-learn mailing list