Bug 7872 – dmd should warn if `printf` is used on D strings
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-09T08:19:32Z
Last change time
2020-03-21T03:56:40Z
Assigned to
No Owner
Creator
Jonas H.
Comments
Comment #0 by jonas — 2012-04-09T08:19:32Z
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.
Comment #1 by bearophile_hugs — 2012-04-09T09:36:00Z
(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);
}
Comment #2 by jonas — 2012-04-09T11:26:57Z
Yeah but I guess it's a common mistake for someone coming from C/C++ so there should be some guidance anyway.