Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 14, 2006 Array slicing | ||||
---|---|---|---|---|
| ||||
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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | "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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | 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 Re: Array slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund |
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.
|
Copyright © 1999-2021 by the D Language Foundation