Thread overview
why static keyword for array necessary
Mar 14, 2005
nix
Mar 14, 2005
jicman
Mar 14, 2005
nix
March 14, 2005
Hello,

import std.stdio;

int main() {

static char[5][2] string = ["house","car"]; writefln("string[0] = %s",string[0]); writefln("string[1] = %s",string[1]);

return 0; }

Why is the static keyword for "char[5][2] string =["house","car"];" necessary?




March 14, 2005
I have the same question.  Also, why can't I initialize a non-static array with values at declare time? ie.

char[][] string = ["house","car"];
..
more code
..
string.length = string.length + 1;
string[string.length - 1] = "dog";

thanks.

jic

nix says...
>
>Hello,
>
>import std.stdio;
>
>int main() {
>
>static char[5][2] string = ["house","car"]; writefln("string[0] = %s",string[0]); writefln("string[1] = %s",string[1]);
>
>return 0; }
>
>Why is the static keyword for "char[5][2] string =["house","car"];" necessary?
>
>
>
>


March 14, 2005
jicman wrote:

>>Why is the static keyword for "char[5][2] string =["house","car"];" necessary? 

> I have the same question.  Also, why can't I initialize a non-static
> array with values at declare time? ie.


Since D does not yet support dynamic population of arrays...
It's a missing feature, possibly even not until D 2.0 ! :-(

Meanwhile, you need to fill out the arrays at run-time instead...
(BTW; this extra step is needed with Java arrays and hashes too)

char[][] strings;
strings.length = 2;
strings[0] = "house";
strings[1] = "car";

You can do it in a "static this() { }" block, to make it automatic
to populate e.g. global variables or calculated "constant" fields.


> more code
> ..
> string.length = string.length + 1;
> string[string.length - 1] = "dog";

You can use this instead, if you like: (less typing, and easier too)

strings ~= "dog";

--anders

PS. I changed "string" into "strings", plural because of the array?
March 14, 2005
Thank you for your answer. I have found this link from March 2004

http://www.digitalmars.com/drn-bin/wwwnews?D/26695

Ok we wait another year :-(


In article <d13ntd$19b9$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
> 
>jicman wrote:
> 
>>>Why is the static keyword for "char[5][2] string =["house","car"];" necessary?
> 
>> I have the same question.  Also, why can't I initialize a non-static array with values at declare time? ie.
> 
> 
>Since D does not yet support dynamic population of arrays... It's a missing feature, possibly even not until D 2.0 ! :-(
> 
>Meanwhile, you need to fill out the arrays at run-time instead... (BTW; this extra step is needed with Java arrays and hashes too)
> 
>char[][] strings; strings.length = 2; strings[0] = "house"; strings[1] = "car";
> 
>You can do it in a "static this() { }" block, to make it automatic to populate e.g. global variables or calculated "constant" fields.
> 
> 
>> more code .. string.length = string.length + 1; string[string.length - 1] = "dog";
> 
>You can use this instead, if you like: (less typing, and easier too)
> 
>strings ~= "dog";
> 
>--anders
> 
>PS. I changed "string" into "strings", plural because of the array?