dynamic array of strings

Denis Koroskin 2korden at gmail.com
Tue Dec 30 09:26:02 PST 2008


On Tue, 30 Dec 2008 20:23:07 +0300, Michael P. <baseball.mjp at gmail.com> wrote:

> Denis Koroskin Wrote:
>
>> On Tue, 30 Dec 2008 18:56:08 +0300, Michael P. <baseball.mjp at gmail.com>  
>> wrote:
>>
>> > Jarrett Billingsley Wrote:
>> >
>> >> On Mon, Dec 29, 2008 at 10:36 PM, 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
>> >> >    {
>> >> names ~= currentName;
>> >> >    }
>> >> >  }
>> >> >  foreach( name; names )
>> >> >  {
>> >> >    writefln( name );
>> >> >  }
>> >> > }
>> >>
>> >> ~= performs an append.  It adds the given item to the end of the  
>> array.
>> >
>> > Under 1.036, this does not work.
>> >
>> > Input:
>> > michael
>> > is
>> > cool
>> > stop
>> >
>> > The output is:
>> > stopeal
>> > st
>> > stop
>> >
>> > What seems to be happening is that the letters of stop are added to  
>> the
>> > word, and if the word is longer than stop, then the rest of the  
>> letters
>> > of the word are added.
>> > So, I'm not sure if this is a bug or what...
>> > -Michael P.
>>
>> Now, it's not. What you do is overwrite the string again and again.
>> Try putting "currentName = null;" before doing din.readf().
>
> Thanks, that worked.
> -Michael P.

Did you understand the difference? Can you explain the behavior (to yourself)?


More information about the Digitalmars-d-learn mailing list