Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 25, 2006 need help | ||||
---|---|---|---|---|
| ||||
int main( char[][] args ) { foreach( char[] arg; args[1 .. args.length] ) { printf( "%.*s ", arg ); } for ( int i = 0; i < args.length; i++ ) { printf("%.*s ", args[i] ); } return 0; } The first printf can't display the information, and the second do. I use dmd 0.154, XP SP2. |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Wang | "Boris Wang" <nano.kago@hotmail.com> wrote in message news:e2k46n$1dkm$1@digitaldaemon.com... > The first printf can't display the information, and the second do. Uhh, yes it does. For me, anyway. But I think the real question here is - why are you using printf instead of writef? ;) int main( char[][] args ) { foreach( char[] arg; args[1 .. args.length] ) { writef( arg, ' ' ); } writefln(); for ( int i = 1; i < args.length; i++ ) { writef( args[i], ' ' ); } return 0; } (These print identical lines for me.) |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Wang | On Tue, 25 Apr 2006 11:13:58 +0800, Boris Wang <nano.kago@hotmail.com> wrote:
> int main( char[][] args )
> {
> foreach( char[] arg; args[1 .. args.length] )
> {
> printf( "%.*s ", arg );
> }
>
> for ( int i = 0; i < args.length; i++ )
> {
> printf("%.*s ", args[i] );
> }
>
> return 0;
> }
>
> The first printf can't display the information, and the second do.
>
> I use dmd 0.154, XP SP2.
The first one skips the first arg, to include the first arg use:
foreach( char[] arg; args[0 .. args.length] )
{
printf( "%.*s ", arg );
}
Apart from that they produce identical output for me.
Regan
|
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | You are right, the args array begin from index 0, but > foreach( char[] arg; args[0 .. args.length] ) This should not be right, it should be : foreach( char[] arg; args[0 .. args.length - 1] ) and more, when i run the followed code: import std.stdio; int main( char[][] args ) { foreach( char[] arg; args[0 .. args.length - 1 ] ) { printf( "%s ", cast(char*)arg ); } } it even produce a assert: Error: AssertError Failure hello_my.d(10) "Regan Heath" <regan@netwin.co.nz> ??????:ops8jr1mr223k2f5@nrage.netwin.co.nz... > On Tue, 25 Apr 2006 11:13:58 +0800, Boris Wang <nano.kago@hotmail.com> wrote: >> int main( char[][] args ) >> { >> foreach( char[] arg; args[1 .. args.length] ) >> { >> printf( "%.*s ", arg ); >> } >> >> for ( int i = 0; i < args.length; i++ ) >> { >> printf("%.*s ", args[i] ); >> } >> >> return 0; >> } >> >> The first printf can't display the information, and the second do. >> >> I use dmd 0.154, XP SP2. > > The first one skips the first arg, to include the first arg use: > > foreach( char[] arg; args[0 .. args.length] ) > { > printf( "%.*s ", arg ); > } > > Apart from that they produce identical output for me. > > Regan |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Wang | The suitable explain is : args[ leftLimit .. rightLimit ] the leftLimit is the first index, and the rightLimit is the last index + 1, just equal with args.length. but we used to write the following code: for ( int i = 0; i < len - 1; i ++ ) .... "Boris Wang" <nano.kago@hotmail.com> дÈëÏûÏ¢ÐÂÎÅ:e2kdlg$1p04$1@digitaldaemon.com... > You are right, the args array begin from index 0, but > >> foreach( char[] arg; args[0 .. args.length] ) > > This should not be right, it should be : > foreach( char[] arg; args[0 .. args.length - 1] ) > > and more, when i run the followed code: > > import std.stdio; > > int main( char[][] args ) > { > foreach( char[] arg; args[0 .. args.length - 1 ] ) > { > printf( "%s ", cast(char*)arg ); > } > } > > it even produce a assert: > > Error: AssertError Failure hello_my.d(10) > > > "Regan Heath" <regan@netwin.co.nz> ??????:ops8jr1mr223k2f5@nrage.netwin.co.nz... >> On Tue, 25 Apr 2006 11:13:58 +0800, Boris Wang <nano.kago@hotmail.com> wrote: >>> int main( char[][] args ) >>> { >>> foreach( char[] arg; args[1 .. args.length] ) >>> { >>> printf( "%.*s ", arg ); >>> } >>> >>> for ( int i = 0; i < args.length; i++ ) >>> { >>> printf("%.*s ", args[i] ); >>> } >>> >>> return 0; >>> } >>> >>> The first printf can't display the information, and the second do. >>> >>> I use dmd 0.154, XP SP2. >> >> The first one skips the first arg, to include the first arg use: >> >> foreach( char[] arg; args[0 .. args.length] ) >> { >> printf( "%.*s ", arg ); >> } >> >> Apart from that they produce identical output for me. >> >> Regan > > |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley Attachments: | Jarrett Billingsley schrieb am 2006-04-25: > "Boris Wang" <nano.kago@hotmail.com> wrote in message news:e2k46n$1dkm$1@digitaldaemon.com... >> The first printf can't display the information, and the second do. > > Uhh, yes it does. For me, anyway. > > But I think the real question here is - why are you using printf instead of writef? ;) <snip> > for ( int i = 1; i < args.length; i++ ) > { > writef( args[i], ' ' ); > } That's usually not a good idea(unchecked format strings...), instead use: for ( int i = 1; i < args.length; i++ ) { writef("%s ", args[i]); } Thomas |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Wang | On Tue, 25 Apr 2006 13:55:21 +0800, Boris Wang <nano.kago@hotmail.com> wrote: > You are right, the args array begin from index 0, but > >> foreach( char[] arg; args[0 .. args.length] ) > > This should not be right, it should be : > foreach( char[] arg; args[0 .. args.length - 1] ) That is not true. In D, arrays are indexed from 0 and when slicing the start index is inclusive and the end index is not. Meaning, for example that: args[0..2] is a slice of items 0, and 1, but _not_ 2. If you use args.length-1 you will _not_ include the last item of the array in the slice. If you want the whole array then the end index must be the array length (AKA the index of the item past the end of the array) > and more, when i run the followed code: > > import std.stdio; > > int main( char[][] args ) > { > foreach( char[] arg; args[0 .. args.length - 1 ] ) > { > printf( "%s ", cast(char*)arg ); > } > } > > it even produce a assert: > > Error: AssertError Failure hello_my.d(10) The assert has nothing to do with the array slice and everything to do with the missing return value in main. Try: import std.stdio; int main( char[][] args ) { foreach( char[] arg; args[0 .. args.length] ) { printf( "%s ", cast(char*)arg ); } return 0; } However, you should be aware that using cast(char*) on a char[] and passing the result to printf is dangerous. It's only working in this case because the arguments are already null terminated, try this: import std.file; import std.string; int main( char[][] args ) { foreach( int i,char[] arg; splitlines(cast(char[])read("bug11.txt")) ) { printf( "%d. %s\n", i,cast(char*)arg ); } return 0; } (you'll need to create a bug11.txt containing a few lines of text) you should notice that the first printf shows the entire file, this is because the splitlines array contains non-null terminated char[] arrays. To safely pass them to functions expecting char* you should use toStringz, eg. import std.file; import std.string; int main( char[][] args ) { foreach( int i,char[] arg; splitlines(cast(char[])read("bug11.txt")) ) { printf( "%d. %s\n", i,toStringz(arg) ); } return 0; } Regan |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Wang | On Tue, 25 Apr 2006 14:10:26 +0800, Boris Wang <nano.kago@hotmail.com> wrote: > The suitable explain is : > args[ leftLimit .. rightLimit ] > > the leftLimit is the first index, and the rightLimit is the > last index + 1, just equal with args.length. Or rather, that rightLimit is one past the last item you want to include. To include the entire array you use 0 and array.length. > but we used to write the following code: > > for ( int i = 0; i < len - 1; i ++ ) To access items in the slice args[0..args.length] you would use this for loop: for(int i = 0; i < args.length; i++) {} or one of these foreach statements: foreach(int i, char[] arg; args) {} foreach(char[] arg; args) {} foreach(arg; args) {} Regan > "Boris Wang" <nano.kago@hotmail.com> дÈëÏûÏ¢ÐÂÎÅ:e2kdlg$1p04$1@digitaldaemon.com... >> You are right, the args array begin from index 0, but >> >>> foreach( char[] arg; args[0 .. args.length] ) >> >> This should not be right, it should be : >> foreach( char[] arg; args[0 .. args.length - 1] ) >> >> and more, when i run the followed code: >> >> import std.stdio; >> >> int main( char[][] args ) >> { >> foreach( char[] arg; args[0 .. args.length - 1 ] ) >> { >> printf( "%s ", cast(char*)arg ); >> } >> } >> >> it even produce a assert: >> >> Error: AssertError Failure hello_my.d(10) >> >> >> "Regan Heath" <regan@netwin.co.nz> >> ??????:ops8jr1mr223k2f5@nrage.netwin.co.nz... >>> On Tue, 25 Apr 2006 11:13:58 +0800, Boris Wang <nano.kago@hotmail.com> >>> wrote: >>>> int main( char[][] args ) >>>> { >>>> foreach( char[] arg; args[1 .. args.length] ) >>>> { >>>> printf( "%.*s ", arg ); >>>> } >>>> >>>> for ( int i = 0; i < args.length; i++ ) >>>> { >>>> printf("%.*s ", args[i] ); >>>> } >>>> >>>> return 0; >>>> } >>>> >>>> The first printf can't display the information, and the second do. >>>> >>>> I use dmd 0.154, XP SP2. >>> >>> The first one skips the first arg, to include the first arg use: >>> >>> foreach( char[] arg; args[0 .. args.length] ) >>> { >>> printf( "%.*s ", arg ); >>> } >>> >>> Apart from that they produce identical output for me. >>> >>> Regan >> >> > > |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Best regards to you! Thanks "Regan Heath" <regan@netwin.co.nz> ??????:ops8jx4rqc23k2f5@nrage.netwin.co.nz... > On Tue, 25 Apr 2006 13:55:21 +0800, Boris Wang <nano.kago@hotmail.com> wrote: >> You are right, the args array begin from index 0, but >> >>> foreach( char[] arg; args[0 .. args.length] ) >> >> This should not be right, it should be : >> foreach( char[] arg; args[0 .. args.length - 1] ) > > That is not true. > > In D, arrays are indexed from 0 and when slicing the start index is inclusive and the end index is not. Meaning, for example that: > > args[0..2] > > is a slice of items 0, and 1, but _not_ 2. If you use args.length-1 you will _not_ include the last item of the array in the slice. If you want the whole array then the end index must be the array length (AKA the index of the item past the end of the array) > >> and more, when i run the followed code: >> >> import std.stdio; >> >> int main( char[][] args ) >> { >> foreach( char[] arg; args[0 .. args.length - 1 ] ) >> { >> printf( "%s ", cast(char*)arg ); >> } >> } >> >> it even produce a assert: >> >> Error: AssertError Failure hello_my.d(10) > > The assert has nothing to do with the array slice and everything to do with the missing return value in main. Try: > > import std.stdio; > > int main( char[][] args ) > { > foreach( char[] arg; args[0 .. args.length] ) > { > printf( "%s ", cast(char*)arg ); > } > return 0; > } > > However, you should be aware that using cast(char*) on a char[] and passing the result to printf is dangerous. It's only working in this case because the arguments are already null terminated, try this: > > import std.file; > import std.string; > > int main( char[][] args ) > { > foreach( int i,char[] arg; splitlines(cast(char[])read("bug11.txt")) ) > { > printf( "%d. %s\n", i,cast(char*)arg ); > } > return 0; > } > > (you'll need to create a bug11.txt containing a few lines of text) > > you should notice that the first printf shows the entire file, this is because the splitlines array contains non-null terminated char[] arrays. To safely pass them to functions expecting char* you should use toStringz, eg. > > import std.file; > import std.string; > > int main( char[][] args ) > { > foreach( int i,char[] arg; splitlines(cast(char[])read("bug11.txt")) ) > { > printf( "%d. %s\n", i,toStringz(arg) ); > } > return 0; > } > > Regan |
April 25, 2006 Re: need help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | "Regan Heath" <regan@netwin.co.nz> ??????:ops8jx4rqc23k2f5@nrage.netwin.co.nz... > On Tue, 25 Apr 2006 13:55:21 +0800, Boris Wang <nano.kago@hotmail.com> wrote: >> You are right, the args array begin from index 0, but >> >>> foreach( char[] arg; args[0 .. args.length] ) >> >> This should not be right, it should be : >> foreach( char[] arg; args[0 .. args.length - 1] ) > > That is not true. > > In D, arrays are indexed from 0 and when slicing the start index is inclusive and the end index is not. Meaning, for example that: > > args[0..2] > > is a slice of items 0, and 1, but _not_ 2. If you use args.length-1 you will _not_ include the last item of the array in the slice. If you want the whole array then the end index must be the array length (AKA the index of the item past the end of the array) > >> and more, when i run the followed code: >> >> import std.stdio; >> >> int main( char[][] args ) >> { >> foreach( char[] arg; args[0 .. args.length - 1 ] ) >> { >> printf( "%s ", cast(char*)arg ); >> } >> } >> >> it even produce a assert: >> >> Error: AssertError Failure hello_my.d(10) > > The assert has nothing to do with the array slice and everything to do with the missing return value in main. Try: > > import std.stdio; > > int main( char[][] args ) > { > foreach( char[] arg; args[0 .. args.length] ) > { > printf( "%s ", cast(char*)arg ); > } > return 0; > } > Why not a complie error, but a runtime error? > However, you should be aware that using cast(char*) on a char[] and passing the result to printf is dangerous. It's only working in this case because the arguments are already null terminated, try this: > > import std.file; > import std.string; > > int main( char[][] args ) > { > foreach( int i,char[] arg; splitlines(cast(char[])read("bug11.txt")) ) > { > printf( "%d. %s\n", i,cast(char*)arg ); > } > return 0; > } > > (you'll need to create a bug11.txt containing a few lines of text) > > you should notice that the first printf shows the entire file, this is because the splitlines array contains non-null terminated char[] arrays. To safely pass them to functions expecting char* you should use toStringz, eg. > > import std.file; > import std.string; > > int main( char[][] args ) > { > foreach( int i,char[] arg; splitlines(cast(char[])read("bug11.txt")) ) > { > printf( "%d. %s\n", i,toStringz(arg) ); > } > return 0; > } > > Regan |
Copyright © 1999-2021 by the D Language Foundation