Search code examples
arraysmultidimensional-arrayreferencedbyref

D - store reference to subarray in variable


I have code like this:

int[][] array = [[1, 2], [3, 4]];
auto line = array[0];
line ~= 5;

I assume line contains a copy of subarray and array is not modified when line is. Is it possible to store subarray as reference?


Solution

  • You could use a pointer to the first element of array:

    int[][] array = [[1, 2], [3, 4]];
    auto line = &array[0];
    *line ~= 5;