www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assigning to char[N]

reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
import std.stdio;

void main()
{
     char[100] a = "old content";
     a = "new content";
}

The program above causes an exception to be thrown:

object.Exception src/rt/arraycat.d(31): lengths don't match for array copy

I admit that a fixed-length char array is not really a string. But 
assuming that the new content is legal UTF-8, what is the best way of 
modifying that array?

Thank you,
Ali
Feb 01 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 what is the best way of modifying that array?
In your code it's the definition too that throws an exception: 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
Feb 01 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Feb 01 2012
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Ali:

      a["one".length .. $] = '\0';
Is DMD able to optimize that string literal away? (Some assembly may be required).
      // 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
Try harder. Try to slice that a. Bye, bearophile
Feb 01 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/01/2012 04:34 PM, bearophile wrote:
 Ali:

       a["one".length .. $] = '\0';
Is DMD able to optimize that string literal away? (Some assembly may
be required). I sure hope so. "one".length should be a compile time constant.
       // 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
Try harder. Try to slice that a. Bye, bearophile
First of all, the parameters in my copy() call were swapped above. It should have been source then destination: copy("new content", a); But it won't work, because strings are ranges of Unicode code points (i.e. dchar ranges). Even narrow strings are exposed as dchar ranges. So the source is an InputRange in that call, and the destination is an output range, but their element types don't match. The following works because now all of the elements are dchar: dchar[100] a = '\0'; copy("new content"d, a[]); Ali
Feb 01 2012
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
OT: Just saw fill() by accident and something caught my eye:

char[100] a;
fill(a[], "bla");  // fail, ok

int[100] a;
fill(a[], "bla");  // works

It could be a constraint issue. To bugzilla?
Feb 01 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/01/2012 04:24 PM, Andrej Mitrovic wrote:
 OT: Just saw fill() by accident and something caught my eye:

 char[100] a;
 fill(a[], "bla");  // fail, ok

 int[100] a;
 fill(a[], "bla");  // works

 It could be a constraint issue.
Although "bla" is an array of char, it is a range of dchar; and dchar automatically converts to int.
 To bugzilla?
I don't think so, because it's just dchar to int conversion. Ali
Feb 01 2012
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 02 Feb 2012 00:38:26 -0500, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
 On 02/01/2012 04:24 PM, Andrej Mitrovic wrote:
  > OT: Just saw fill() by accident and something caught my eye:
  >
  > char[100] a;
  > fill(a[], "bla");  // fail, ok
  >
  > int[100] a;
  > fill(a[], "bla");  // works
  >
  > It could be a constraint issue.

 Although "bla" is an array of char, it is a range of dchar; and dchar =
=
 automatically converts to int.
But this is the kind of unintuitive shit that treating char arrays not a= s = arrays in *some* parts of D causes great confusion and silly limitations= . = The above should be the opposite, the first line should pass and the = second should fail.
  > To bugzilla?

 I don't think so, because it's just dchar to int conversion.
You are likely to get an "invalid" response due to the strict views of = Andrei, but I think it's unacceptable behavior for fill to say it cannot= = fill a string with string data. -Steve
Feb 06 2012