Thread overview
Slices help
Sep 08, 2003
kw
Sep 08, 2003
DeadCow
Sep 08, 2003
Mike Wynn
Sep 08, 2003
kw
Sep 08, 2003
Daniel Yokomiso
September 08, 2003
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


September 08, 2003
"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


September 08, 2003
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)

September 08, 2003
"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


September 08, 2003
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.