Using "strcpy" to assign value to dynamic char array

pascal111 judas.the.messiah.111 at gmail.com
Mon Nov 1 20:49:53 UTC 2021


On Monday, 1 November 2021 at 20:15:14 UTC, Steven Schveighoffer 
wrote:
> 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

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


More information about the Digitalmars-d-learn mailing list