static array of pointers to dynamic arrays of ints problem...

WhatMeForget kheaser at gmail.com
Sun Apr 22 06:00:15 UTC 2018


Surely a stupid mistake on my part, but why is the first array 
repeated?


import std.stdio;

void main()
{
     int[]*[2] a;  // a static arrray holding pointers to dynamic 
arrays

     static int unique = 0;

     foreach(i, elem; a)
     {
         int[] temp = new int[](5);
         foreach(ref element; temp)
         {
             element = unique;
             unique++;
         }
         writeln("temp = ", temp);
         a[i] = &temp;
     }

     foreach(i, elem; a)
     {
             writeln(a[i].length);
             writeln("[", i, "][]", *a[i]);
     }
}

temp = [0, 1, 2, 3, 4]
temp = [5, 6, 7, 8, 9]
5
[0][][5, 6, 7, 8, 9]
5
[1][][5, 6, 7, 8, 9]


More information about the Digitalmars-d-learn mailing list