D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6305 - String literals don't always have a 0 appended to them
Summary: String literals don't always have a 0 appended to them
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: Other Windows
: P2 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-13 01:57 UTC by Denis Shelomovskii
Modified: 2011-07-13 12:22 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Denis Shelomovskii 2011-07-13 01:57:25 UTC
According to http://www.digitalmars.com/d/1.0/arrays.html#printf
"String literals already have a 0 appended to them"
But if we create static arrays, they are exactly one after another: 

import std.stdio;
const s1 = "abcd", s2 = "EFG", s3 = "h";
void main() {
    writefln("%s %s %s\n%s %s %s",
        s1, *(s1.ptr + s1.length), cast(int) *(s1.ptr + s1.length),
        s2, *(s2.ptr + s2.length), cast(int) *(s2.ptr + s2.length)
    );
}

Prints:
abcd E 69
EFG h 104

If I missed something, I think this "something" should be mentioned in documentation near citation above.
Comment 1 Steven Schveighoffer 2011-07-13 05:35:41 UTC
This seems to be windows-specific.  The exact code produces this output on linux (as far back as 2.033, the earliest installed compiler I have):

abcd  0
EFG  0
Comment 2 Denis Shelomovskii 2011-07-13 05:40:27 UTC
(In reply to comment #1)
> This seems to be windows-specific.  The exact code produces this output on
> linux (as far back as 2.033, the earliest installed compiler I have):
> 
> abcd  0
> EFG  0

It is a D1 only issue.
Comment 3 Steven Schveighoffer 2011-07-13 05:58:14 UTC
Oops, sorry for the noise.
Comment 4 Steven Schveighoffer 2011-07-13 12:22:48 UTC
Actually, I did have a D1 compiler laying around.  And I figured out the issue.

The issue is D1's type inference treats string literal types as char[N] where N is a uint.  Note that D2's type inference treats string literals as immutable(char)[].  So the issue is that you are not declaring a type, and D is assuming you meant it to be a fixed-sized array.  So the literal *does* have a zero, but it is copied into your declared fixed-sized arrays in the global segment without the zero.

I figured it out by doing:

pragma(msg, typeof(s1).stringof);

which prints:
char[4u];

If you do this:

const string s1 = "abcd", s2 = "EFG", s3 = "h";

Then it works as you expect.