www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array slicing

reply Heinz <malagana15 yahoo.es> writes:
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
next sibling parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
prev sibling next sibling parent reply Georg Wrede <georg.wrede nospam.org> writes:
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
parent reply Heinz <malagana15 yahoo.es> writes:
"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
parent torhu <fake address.dude> writes:
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
prev sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
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
parent reply =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= <afb algonet.se> writes:
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
parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
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