string foo = "john"; printf("hello %s\n", foo); doesn't work because `printf` expects a zero-terminated string. The compiler should really yield a warning here.
(In reply to comment #0) > string foo = "john"; > printf("hello %s\n", foo); > > doesn't work because `printf` expects a zero-terminated string. > > The compiler should really yield a warning here. In D string literals are zero-terminated. So this run correctly: import core.stdc.stdio; void main() { string foo = "john"; printf("hello %s\n", foo.ptr); }
Yeah but I guess it's a common mistake for someone coming from C/C++ so there should be some guidance anyway.
(In reply to comment #1) > In D string literals are zero-terminated. So this run correctly: > If documentation (http://www.prowiki.org/wiki4d/wiki.cgi?action=browse&id=DanielKeep/TextInD&oldid=StringsInD) is not mistaken string literals are zero-terminated in DMD, thus it is compiler-specific feature.
For the record, some discussion on this is happening in the forum too. http://forum.dlang.org/thread/ytoebhnapmcixfdtaoqd@forum.dlang.org
DMD should warn if printf is used full stop. :)
(In reply to comment #3) > (In reply to comment #1) > > In D string literals are zero-terminated. So this run correctly: > > > > If documentation > (http://www.prowiki.org/wiki4d/wiki.cgi?action=browse&id=DanielKeep/TextInD&oldid=StringsInD) > is not mistaken string literals are zero-terminated in DMD, thus it is > compiler-specific feature. I just found that here (http://dlang.org/interfaceToC.html) is written that they actually are zero-terminated in D.
it is documented that string literals are zero terminated. In addition warnings are not in the tradition of the compiler.