www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does D support dynamically allocated multi-dimensional arrays?

reply Seth Hoenig <seth.a.hoenig gmail.com> writes:
--0022152d5fb5f01573048de778d7
Content-Type: text/plain; charset=ISO-8859-1

What I really want is something like this:

auto s = args[1];
auto t = args[2];
auto d = new int[s.length][t.length];

but the compiler complains with the error: Error: Integer constant
expression expected instead of s.length

So then I try to fudge it with something like:

auto d = new int[][t.length];
d.length = s.length;

But then accessing elements of d throws an exception:
core.exception.RangeError Lev(7): Range violation

Soo.. how am I supposed to make a matrix when I don't know the dimensions at
compile time?

--0022152d5fb5f01573048de778d7
Content-Type: text/html; charset=ISO-8859-1

What I really want is something like this:<br><br>auto s = args[1];<br>auto t =
args[2];<br>auto d = new int[s.length][t.length];<br><br>but the compiler
complains with the error: Error: Integer constant expression expected instead
of s.length<br>
<br>So then I try to fudge it with something like:<br><br>auto d = new
int[][t.length];<br>d.length = s.length;<br><br>But then accessing elements of
d throws an exception: core.exception.RangeError Lev(7): Range violation<br>
<br>Soo.. how am I supposed to make a matrix when I don&#39;t know the
dimensions at compile time?<br><br><br>

--0022152d5fb5f01573048de778d7--
Aug 15 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Seth Hoenig <seth.a.hoenig gmail.com> wrote:

 What I really want is something like this:

 auto s = args[1];
 auto t = args[2];
 auto d = new int[s.length][t.length];

 but the compiler complains with the error: Error: Integer constant
 expression expected instead of s.length

 So then I try to fudge it with something like:

 auto d = new int[][t.length];
 d.length = s.length;

 But then accessing elements of d throws an exception:
 core.exception.RangeError Lev(7): Range violation

 Soo.. how am I supposed to make a matrix when I don't know the  
 dimensions at
 compile time?

auto d = new int[][](args[1].length, args[2].length); -- Simen
Aug 15 2010