www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static array error and a surprise!

reply MMagain <MMagain_member pathlink.com> writes:
int[3] var=[1,2,3]; 

gives the error:

main.d(153): variable main.main.var is not a static and cannot have static
initializer

I thought int[3] a would be a static array ?

surprise question :D
(sorry about all the questions)
Is it considered good programming to use the c standard modules (like
std.c.stdio) or should, where possible, the non-c standard library modules(like
std.stdio) be used?
Jun 26 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"MMagain" <MMagain_member pathlink.com> wrote in message 
news:e7pua5$2un9$1 digitaldaemon.com...
 int[3] var=[1,2,3];

 gives the error:

 main.d(153): variable main.main.var is not a static and cannot have static
 initializer

 I thought int[3] a would be a static array ?
This is an ambiguity in the terminology. "static array" can mean either a statically _sized_ array (like int[3]), or a statically _allocated_ array (which is preceded by the 'static' keyword). The compiler is complaining because you can't use array literals with arrays which aren't statically allocated; thus, you must write static int[3] var = [1, 2, 3];
 surprise question :D
 (sorry about all the questions)
If you never ask them, you'll never find out the answers ;)
 Is it considered good programming to use the c standard modules (like
 std.c.stdio) or should, where possible, the non-c standard library 
 modules(like
 std.stdio) be used?
It's probably better to use the D modules (std.stdio). Please, for the LOVE OF GOD, do NOT use printf(). I don't care how much you want to. Use std.stdio.writefln() instead. writefln() is D-aware, typesafe, and doesn't require a format string. The same goes for most of the other D modules. You should really only use the std.c modules when interfacing with C libraries or for features which aren't in the D standard library (though you shouldn't run into that very often).
Jun 26 2006