www.digitalmars.com         C & C++   DMDScript  

D - Is this a printf bug?

reply "Matthew B." <mattcbro earthlink.net> writes:
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
next sibling parent Ant <Ant_member pathlink.com> writes:
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
prev sibling next sibling parent Patrick Down <Patrick_member pathlink.com> writes:
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
prev sibling parent "Walter" <walter digitalmars.com> writes:
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