D - Is this a printf bug?
- "Matthew B." <mattcbro earthlink.net> Oct 16 2003
- Ant <Ant_member pathlink.com> Oct 16 2003
- Patrick Down <Patrick_member pathlink.com> Oct 16 2003
- "Walter" <walter digitalmars.com> Oct 16 2003
This is probably a newbie question, but I consistently get errors when using
the %s identifier in a printf
format string. I thought maybe my problem was that I have to convert the
string to a C-type zero terminated string, but I note that
char* toCharz(char[] string) is deprecated according to the compiler so I
am uncertain.
At any rate here is a simple code example that has problems.
// import c.stdio.lib ;
import string ;
import ctype ;
import stream ;
int main(char[][] args) {
char [] str = "blah blah " ;
printf("A standard D string: %s\n", str) ;
printf("Input argument passed: %s \n",args[1]) ;
return(0) ;
}
I compile it using "dmd test.d" and then run
"test blarg" in a dos window. The error I see becomes
A standard D string: Error: Access Violation
I may bore into the source code a bit to see what's going on.
Regards,
Matt B.
Oct 16 2003
In article <bmmp1i$14d0$1 digitaldaemon.com>, Matthew B. says...int main(char[][] args) { char [] str = "blah blah " ; printf("A standard D string: %s\n", str) ; printf("Input argument passed: %s \n",args[1]) ;
D string aren't null terminated they have the lenght then the string. (this should be easy to find on the D pages) use printf("Input argument passed: %.*s \n",args[1]) ; instead. Ant PS (not to you, Matthew B., to the other guys) seems we also need a newby group :)
Oct 16 2003
http://www.prowiki.org/wiki4d/wiki.cgi?action=browse&id=FaqRoadmap&oldid=FAQ#ErrorAccessViolationonprintingastring In article <bmmp1i$14d0$1 digitaldaemon.com>, Matthew B. says...This is probably a newbie question, but I consistently get errors when using the %s identifier in a printf format string. I thought maybe my problem was that I have to convert the string to a C-type zero terminated string, but I note that char* toCharz(char[] string) is deprecated according to the compiler so I am uncertain. At any rate here is a simple code example that has problems. // import c.stdio.lib ; import string ; import ctype ; import stream ; int main(char[][] args) { char [] str = "blah blah " ; printf("A standard D string: %s\n", str) ; printf("Input argument passed: %s \n",args[1]) ; return(0) ; } I compile it using "dmd test.d" and then run "test blarg" in a dos window. The error I see becomes A standard D string: Error: Access Violation I may bore into the source code a bit to see what's going on. Regards, Matt B.
Oct 16 2003
I should have added this to the FAQ long ago; anyway, it's there now! www.digitalmars.com/d/faq.html#printf
Oct 16 2003









Ant <Ant_member pathlink.com> 