Using "strcpy" to assign value to dynamic char array

Ali Çehreli acehreli at yahoo.com
Mon Nov 1 21:01:31 UTC 2021


On 11/1/21 1:49 PM, pascal111 wrote:

 > Yes, I'm practicing doing things in low level style like standard C.

All you needed extra was to let the slice know about the new length:

import std.stdio;
import core.stdc.string;

void main() {
   char[] s="xyz".dup;
   strcpy(&s[0], "Hello World!");
   s = s.ptr[0..12];           // <-- Here
   writeln(s);
}

Now, the result is "correct" without dup:

Hello World!

The program is as incorrect as its C equivalent would be. ;)

Ali



More information about the Digitalmars-d-learn mailing list