What exactly are the String literrals in D and how they work?
jfondren
julian.fondren at gmail.com
Sun Aug 15 08:47:39 UTC 2021
On Sunday, 15 August 2021 at 08:11:39 UTC, rempas wrote:
> On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote:
>> ```d
>> unittest {
>> char* s = "John".dup.ptr;
>> s[0] = 'X'; // no segfaults
>> assert(s[0..4] == "Xohn"); // ok
>> }
>> ```
>>
>
> Well, that one didn't worked out really well for me. Using
> `.dup.ptr`, didn't added a null terminated character
dup() isn't aware of the NUL since that's outside the slice of
the string. It only copies the chars in "John". You can use
toStringz to ensure NUL termination:
https://dlang.org/phobos/std_string.html#.toStringz
More information about the Digitalmars-d-learn
mailing list