www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6305] New: String literals doesn't already have a 0 appended to them

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6305

           Summary: String literals doesn't already have a 0 appended to
                    them
           Product: D
           Version: D1
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: verylonglogin.reg gmail.com



---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 13 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6305


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com



05:35:41 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6305




---

 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6305


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|schveiguy yahoo.com         |



05:58:14 PDT ---
Oops, sorry for the noise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 13 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6305


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy yahoo.com
         Resolution|                            |INVALID



12:22:48 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 13 2011