Assigning to char[N]

Ali Çehreli acehreli at yahoo.com
Wed Feb 1 16:00:00 PST 2012


On 02/01/2012 03:14 PM, bearophile wrote:
 > Ali Çehreli:
 >
 >> what is the best way of modifying that array?
 >
 > In your code it's the definition too that throws an exception:

Of course! I need sleep. :( I also failed to mention that the rest of 
the characters should be '\0' filled too but it's not a big deal.

 > void main() {
 >      char[100] a = "old content";
 > }
 >
 >
 > This works, but it's not nice:
 >
 > import std.stdio;
 > void main() {
 >      char[100] a;
 >      string s1 = "old content";
 >      a[0 .. s1.length] = s1;
 >      writeln(a);
 >      string s2 = "new content";
 >      a[0 .. s2.length] = s2;
 >      writeln(a);
 >      a[0 .. "new content".length] = "new content";
 >      writeln(a);
 > }
 >
 >
 > Bye,
 > bearophile

The following is some of mine and the ones that did not work.

import std.stdio;
import std.algorithm;
import std.string;
import std.array;

void main()
{
     char[100] a;

     // explicit:
     foreach (i, c; "one") {
         a[i] = c;
     }
     a["one".length .. $] = '\0';
     writeln(a);

     a = leftJustify("two", a.length, '\0');
     writeln(a);

     // copy(a, "new content");
     // fill(a, "new content");
     // insertInPlace(a, "new content");
     //
     // Those do not work with errors similar to this:
     //
     //   Error: template std.algorithm.copy(Range1,Range2) if
     //   (isInputRange!(Range1) &&
     //   isOutputRange!(Range2,ElementType!(Range1))) does not match any
     //   function template declaration
}

Ali



More information about the Digitalmars-d-learn mailing list