How to create a mutable array of strings?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 17 02:21:56 PDT 2015


On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>
> On Sun, 17 May 2015 09:06:38 +0000
> Dennis Ritchie via Digitalmars-d-learn 
> <digitalmars-d-learn at puremagic.com>
> wrote:
>
>> Hi,
>> It seems to me, or D do not create mutable array of strings?
>> 
>> How to create a mutable equivalent of a string array?
>> 
>> -----
>> string[] s = ["foo", "bar"];
>> // s[1][1] = 't'; // immutable expression s[1][1]
>
> or you can use cast if you are sure thats what you really need:
> auto s = [cast(char[])"foo", cast(char[])"bar"];
>
> or
>
> auto s = cast(char[][])["foo", "bar"];

That's not a good idea. I haven't checked, but this will likely 
segfault on mutation, because the string literals are placed into 
read-only memory. And multiple identical strings are merged 
(shared), so that this would modify all occurances of that string 
literal, even if it shouldn't segfault.


More information about the Digitalmars-d-learn mailing list