dynamic array of strings

Tim M a at b.com
Mon Dec 29 19:53:58 PST 2008


On Tue, 30 Dec 2008 16:36:24 +1300, Michael P. <baseball.mjp at gmail.com>  
wrote:

> import std.stdio;
> import std.cstream;
> void main()
> {
>   char[][] names;
>   char[] currentName;
>   while( true )
>   {
>     din.readf( "%s", &currentName );
>     if( currentName == "stop" )
>     {
>       break;
>     }
>     else
>     {
>       //what goes here to dynamically allocate memory for the
>      //names array? new statement? .length?
>     }
>   }
>   foreach( name; names )
>   {
>     writefln( name );
>   }
> }
> How would I go about dynamically allocating memory for the names array  
> in the part that I commented?
> I'm not really sure about the dynamic array details in D, this is just  
> an example to help.
> -Michael P.

Where is din.readf? If it is a c function then it won't be able to set up  
the dynamic array. You can set the length property to allocte the memory  
and pass the c function currentName.ptr. To append currentName to names do  
somehing like:

names ~= currentName;


More information about the Digitalmars-d-learn mailing list