Using "strcpy" to assign value to dynamic char array

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 1 20:15:14 UTC 2021


On 11/1/21 3:56 PM, pascal111 wrote:

> But what if I want to use "strcpy" function to assign that new value to 
> the array that the problem is that the array won't take more than its 
> first initializing value length:
> 
> {
> 
> char[] s="xyz".dup;
> 
> strcpy(&s[0], "Hello World!");
> 
> writeln(s);
> 
> }
> 
> Result:
> 
> Hel
> 

Don't do this, you just corrupted memory! You wrote 13 bytes into a 
memory location that contains 3.

Use `.dup`, it does the equivalent of `strcpy`.

Can you share why you want to use `strcpy` here?

-Steve


More information about the Digitalmars-d-learn mailing list