crash D1 compiler

Simen Kjaeraas simen.kjaras at gmail.com
Sun Feb 1 07:05:29 PST 2009


On Sun, 01 Feb 2009 13:01:09 +0100, Zorran <zorran at tut.by> wrote:

> This code crash D1 compiler (v1.039)
>
> ============
> import std.stdio;
>
> void main()
> {
> 	string ss="sample";
> 	printf("%s", cast(char*)(ss+"\0") );
> }
> ===========

Indeed it does. Now of course, ss + "\0" makes no sense, but the compiler still should not crash.

To correctly concatenate two strings, use the ~ operator. Also, to convert a D string to a C string (char *), use toStringz, which automagically adds the terminating null. Your program would then look like this:

void main() {
	string ss = "sample";
	printf("%s", toStringz(ss));
}

--
Simen


More information about the Digitalmars-d-bugs mailing list