Thread overview
Array slicing
Dec 14, 2006
Heinz
Dec 14, 2006
Oskar Linde
Dec 14, 2006
Georg Wrede
Dec 14, 2006
Heinz
Dec 14, 2006
torhu
Dec 14, 2006
Hasan Aljudy
Dec 14, 2006
Hasan Aljudy
December 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
December 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
December 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.
December 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
December 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
December 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
December 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
December 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.