digitalmars.D.bugs - toString()???
- "Ozy" <ozy 4dm.org> Aug 11 2004
- J C Calvarese <jcc7 cox.net> Aug 11 2004
- h3r3tic <h3r3tic dev.null> Aug 11 2004
- Derek Parnell <derek psych.ward> Aug 11 2004
- "Ozy" <ozy 4dm.org> Aug 11 2004
- "Vathix" <vathixSpamFix dprogramming.com> Aug 11 2004
- "Ozy" <ozy 4dm.org> Aug 12 2004
Hello, there.
I was using function "toString()", and found...
/* test.d */
import std.c.stdio;
import std.stream;
char* str;
void main(){
for(int i=0; i<20; ++i){
str = toString(i);
printf("n=%s\n",str);
}
return;
}
/* end test.d */
result:
test
n=123456789
n=23456789
n=3456789
n=456789
n=56789
n=6789
n=789
n=89
n=9
n=10
n=11
n=12
n=13
n=14
n=15
n=16
n=17
n=18
n=19
Aug 11 2004
Ozy wrote:Hello, there. I was using function "toString()", and found... /* test.d */ import std.c.stdio; import std.stream; char* str; void main(){ for(int i=0; i<20; ++i){ str = toString(i); printf("n=%s\n",str); } return; } /* end test.d */ result:test
n=0123456789 n=123456789 n=23456789 n=3456789 n=456789 n=56789 n=6789 n=789 n=89 n=9 n=10 n=11 n=12 n=13 n=14 n=15 n=16 n=17 n=18 n=19
Interesting, but not entirely unexpected. This might be what you want: /* test.d */ import std.c.stdio; import std.stream; char[] str; /* I wouldn't use a char* unless I had to. */ void main(){ for(int i=0; i<20; ++i){ str = toString(i); printf("n=%.*s\n",str); /* "%.*s" is the specifier for char[]*/ } return; } /* end test.d */ n=0 n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 n=10 n=11 n=12 n=13 n=14 n=15 n=16 n=17 n=18 n=19 There are a couple reasons for this: 1) toString expects to to return a char[]. 2) Weird things can happen when strings are sent to printf without a trailing \0. For some more interesting examples, see: http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/printf -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Aug 11 2004
Ozy wrote:/* test.d */ import std.c.stdio; import std.stream; char* str; void main(){ for(int i=0; i<20; ++i){ str = toString(i); printf("n=%s\n",str); } return; } /* end test.d */
Hiya, your programs has several programs... you're using char* to point to a string. after doing str=toString() you loose the length of the string and you cant give it thru %.*s to printf. and you give it thru normal %s which doesnt work with D strings... The solution is damn simple, though :] !(code) import std.stdio; void main() { for (int i = 0; i < 20; ++i) { writefln("n=", i); } } !(/code) printf should be depreceated for new code imo ;) Tom
Aug 11 2004
On Thu, 12 Aug 2004 11:51:18 +0900, Ozy wrote:Hello, there. I was using function "toString()", and found... /* test.d */ import std.c.stdio; import std.stream; char* str; void main(){ for(int i=0; i<20; ++i){ str = toString(i); printf("n=%s¥n",str); } return; } /* end test.d */ result:test
n=123456789 n=23456789 n=3456789 n=456789 n=56789 n=6789 n=789 n=89 n=9 n=10 n=11 n=12 n=13 n=14 n=15 n=16 n=17 n=18 n=19
If the integer is from 0 to 9, the toString() routine returns a slice into a static array. The correct way to use the return value from toString is more like this ... import std.c.stdio; import std.stream; char[] str; // NOT char*; void main(){ for(int i=0; i<20; ++i) { str = toString(i); printf("n=%.*s\n",str); // NOT %s but %.*s } return; } /* end test.d */ -- Derek Melbourne, Australia 12/Aug/04 1:48:11 PM
Aug 11 2004
I see. Then, if I use C-Library, should I write like this? char[2] str; str[0] = toString(0-9numeral)[0]; str[1] ='\0'; c_func(str); It's troublesome for me... Anyway, I understood that this problem is not a bug. Thank you very much!
Aug 11 2004
"Ozy" <ozy 4dm.org> wrote in message news:cfeu67$1pk3$1 digitaldaemon.com...I see. Then, if I use C-Library, should I write like this? char[2] str; str[0] = toString(0-9numeral)[0]; str[1] ='\0'; c_func(str); It's troublesome for me... Anyway, I understood that this problem is not a bug. Thank you very much!
char[] dstr; char* cstr; dstr = toString(9); cstr = toStringz(dstr); // zero terminate c_func(cstr); // hooray
Aug 11 2004









J C Calvarese <jcc7 cox.net> 