Using "strcpy" to assign value to dynamic char array

pascal111 judas.the.messiah.111 at gmail.com
Mon Nov 1 21:28:50 UTC 2021


On Monday, 1 November 2021 at 21:01:31 UTC, Ali Çehreli wrote:
> 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

This can serve the style I want. It uses OOP style like C++ by 
putting a pointer as a property, but pointers themselves are low 
level.


More information about the Digitalmars-d-learn mailing list