digitalmars.D.learn - Array slicing
- Heinz <malagana15 yahoo.es> Dec 14 2006
- Oskar Linde <oskar.lindeREM OVEgmail.com> Dec 14 2006
- Georg Wrede <georg.wrede nospam.org> Dec 14 2006
- Heinz <malagana15 yahoo.es> Dec 14 2006
- torhu <fake address.dude> Dec 14 2006
- Hasan Aljudy <hasan.aljudy gmail.com> Dec 14 2006
- =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= <afb algonet.se> Dec 14 2006
- Hasan Aljudy <hasan.aljudy gmail.com> Dec 14 2006
Hi, i'm trying to slice an array of chars and print it to the screen but it
doesn't seems to work, here's the code:
/////////////////////////////////////
import std.string;
alias char[] string;
int main(char[][] args)
{
printf("hello world\n");
string hw = "Hello_World";
printf(cast(char*)hw[0 .. 5]);
return 0;
}
/////////////////////////////////////
What could be wrong? it just prints to the output "Hello_World".
Heinz
Dec 14 2006
Heinz wrote:Hi, i'm trying to slice an array of chars and print it to the screen but it doesn't seems to work, here's the code: ///////////////////////////////////// import std.string; alias char[] string; int main(char[][] args) { printf("hello world\n"); string hw = "Hello_World"; printf(cast(char*)hw[0 .. 5]); return 0; } ///////////////////////////////////// What could be wrong? it just prints to the output "Hello_World".
printf expects a zero terminated string. Try: printf(toStringz(hw[0 .. 5])); or why not use writef instead. /Oskar
Dec 14 2006
Heinz wrote:Hi, i'm trying to slice an array of chars and print it to the screen but it doesn't seems to work, here's the code: ///////////////////////////////////// import std.string; alias char[] string; int main(char[][] args) { printf("hello world\n"); string hw = "Hello_World"; printf(cast(char*)hw[0 .. 5]); return 0; } ///////////////////////////////////// What could be wrong? it just prints to the output "Hello_World". Heinz
What is wrong is that the hello.d in samples.d STILL USES PRINTF. :-( That gives you and thousands of others the impression that printf is a natural choice for printing stuff in D.
Dec 14 2006
"That gives you and thousands of others the impression that printf is a natural choice for printing stuff in D." So, what's the real natural choice for printing stuff in D? Thanks
Dec 14 2006
Heinz wrote:"That gives you and thousands of others the impression that printf is a natural choice for printing stuff in D." So, what's the real natural choice for printing stuff in D? Thanks
writef and writefln, in std.stdio. http://www.digitalmars.com/d/phobos/std_stdio.html
Dec 14 2006
printf is not a part of D. use writef instead. import std.stdio; Heinz wrote:Hi, i'm trying to slice an array of chars and print it to the screen but it doesn't seems to work, here's the code: ///////////////////////////////////// import std.string; alias char[] string; int main(char[][] args) { printf("hello world\n"); string hw = "Hello_World"; printf(cast(char*)hw[0 .. 5]); return 0; } ///////////////////////////////////// What could be wrong? it just prints to the output "Hello_World". Heinz
Dec 14 2006
Hasan Aljudy wrote:printf is not a part of D. use writef instead.
The C library is a part of D, so using std.c.stdio.printf is OK as long as toStringz is used. Maybe std.stdio.writef would be easier to use, but both alternatives are valid D. --anders
Dec 14 2006
Anders F Björklund wrote:Hasan Aljudy wrote:printf is not a part of D. use writef instead.
The C library is a part of D, so using std.c.stdio.printf is OK as long as toStringz is used. Maybe std.stdio.writef would be easier to use, but both alternatives are valid D. --anders
I think it's better to just say printf is not a part of D, to not confuse new comers. If you must be politically correct, printf is a part of the C standard library, which is available through the std.c package, but its use is not recommended.
Dec 14 2006









Oskar Linde <oskar.lindeREM OVEgmail.com> 