www.digitalmars.com         C & C++   DMDScript  

D - Slices help

reply kw <kw_member pathlink.com> writes:
Why is the following not working? (DMD 0.71)

int main (char[][] args)
{
char[] s = "12345";

printf("%s --> ", (char *)s);
s = s[0 .. s.length-1]; // I want to strip the last char
printf("%s\n", (char *)s);
return 0;
}

It prints 12345 --> 12345
Sep 07 2003
next sibling parent "DeadCow" <deadcow-remove-this free.fr> writes:
"kw" <kw_member pathlink.com> a écrit dans le message news:
bjgids$1hdv$1 digitaldaemon.com...

 s = s[0 .. s.length-1]; // I want to strip the last char
You are copying array in the same array as if you do : s[0] = s[0]; s[1] = s[1]; ... s[s.length-1] = s[s.length-1]; Try it : s.length--; -- Nicolas Repiquet
Sep 07 2003
prev sibling next sibling parent reply Mike Wynn <mike l8night.co.uk> writes:
kw wrote:
 Why is the following not working? (DMD 0.71)
 
 int main (char[][] args)
 {
 char[] s = "12345";
 
 printf("%s --> ", (char *)s);
 s = s[0 .. s.length-1]; // I want to strip the last char
 printf("%s\n", (char *)s);
 return 0;
 }
 
 It prints 12345 --> 12345
 
 
try using printf( "%.*s", s ); slices are not null terminated s[0..1] will still printf %s as s[0] ... \0 (if you get my meaning)
Sep 07 2003
parent kw <kw_member pathlink.com> writes:
Not working:
 printf("%s\n", (char *)s);
try using
printf( "%.*s", s );   slices are not null terminated s[0..1] will still 
  printf %s as s[0] ... \0 (if you get my meaning)
Thanks! That worked. I few warnings for things that are not very intuitive for C programmers would be a nice addition.
Sep 07 2003
prev sibling parent "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
"kw" <kw_member pathlink.com> escreveu na mensagem
news:bjgids$1hdv$1 digitaldaemon.com...
 Why is the following not working? (DMD 0.71)

 int main (char[][] args)
 {
 char[] s = "12345";

 printf("%s --> ", (char *)s);
 s = s[0 .. s.length-1]; // I want to strip the last char
 printf("%s\n", (char *)s);
 return 0;
 }

 It prints 12345 --> 12345
In D arrays are more than pointers, because they carry length information too. But String literals have a additional invisible \0 in the end. So when you say:
 printf("%s --> ", (char *)s);
You are treating the address/length pair that represents the array as a "char*", so the length info is lost, but the invisible \0 at the end of "12345" is used to terminate the string inside "printf". When you slice the array, you just create a new address/length pair, so the sliced variable
 s = s[0 .. s.length-1]; // I want to strip the last char
Points to the original "12345\0" array, when used as an array it'll give a reduced length. But when you cast it to "char*" the printf will find the \0 only after "5". So you either use "%.*s" instead (that handle "char[]" properly) or use the "toStringz" function from Phobos, that'll give a "char*" with the \0 at the end, so "toStringz(s[0 .. s.length-1])" will return a "char*" pointing a memory location containing "1234\0". Best regards, Daniel Yokomiso. "Lord save me from your followers." --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.514 / Virus Database: 312 - Release Date: 30/8/2003
Sep 07 2003