string literals
Milke Wey
no.spam at example.com
Sat Jan 26 07:48:39 PST 2008
On Sat, 2008-01-26 at 10:26 -0500, bearophile wrote:
> Robert Fraser:
> > Saaa wrote:
> > > char[] str;
> > > char[] str1 = "abc";
> > > str[0] = 'b'; // error, "abc" is read only, may crash
> > > Is this example correct?
> > Yes, because the "abc" is a string literal, that is to say it's written
> > in the code itself. If str1 was loaded from an outside source, such as a
> > file, user input, etc., then you could modify it without issue.
> >
> > For string LITERALS (a string literal is one you write in the code
> > itself, usually encased in double-quotes), modifying them without
> > calling .dup on them is bad. For other strings, it's perfectly okay.
>
> I am no an expert of D yet, but I think in the following D 1.x code str is a dynamic array, so it can be changed safely:
>
> void main() {
> char[] str = "abc";
> str[0] = 'b';
> }
>
> Bye,
> bearophile
On linux you would get an nice Segfault when running that code.
More information about the Digitalmars-d-learn
mailing list