"immutable string" vs "const string*"

Timoses timosesu at gmail.com
Tue Sep 11 10:21:14 UTC 2018


On Sunday, 9 September 2018 at 08:41:37 UTC, Christian Mayer 
wrote:
> On Sunday, 9 September 2018 at 08:14:41 UTC, rikki cattermole 
> wrote:
>
>> Are you aware that a string is just an alias of 
>> immutable(char)[]?
>
> Yes, I'm aware of that. But it's the same, for example, with 
> just one char. "immutable char" vs "const char*".
>
> Or int, or any other data type. As of my current understanding 
> "char" will create a new variable and copy the content of the 
> original to the new variable. "char*" will just use the 
> pointer. And "const char*" is good for when not modifying. But 
> I also can achieve the same using "immutable char". But I'm not 
> sure when better using "immutable char".
>
> In C I would rather use a const pointer. But since I just 
> started learing D I'm not so sure because there are so many 
> ways.

Since strings are slices (immutable(char)[]) it could also be 
worth reading into slices [1]. Assigning an existing slice to 
another slice will not copy the content but only the slice struct 
(length and pointer to data).

[1] https://dlang.org/articles/d-array-article.html


More information about the Digitalmars-d-learn mailing list