Jump to page: 1 2
Thread overview
Question about casts
Mar 19, 2003
Steve Adams
Mar 19, 2003
John Reimer
Mar 19, 2003
Steve Adams
Mar 19, 2003
John Reimer
Mar 19, 2003
Steve Adams
Mar 19, 2003
John Reimer
Mar 19, 2003
Steve Adams
Mar 19, 2003
Russ Lewis
Mar 19, 2003
Patrick Down
Mar 19, 2003
John Reimer
Mar 19, 2003
Steve Adams
Mar 19, 2003
John Reimer
Mar 19, 2003
Steve Adams
Mar 19, 2003
Matthew Wilson
March 19, 2003
Can someone clear this up for me?  The following code doesn't work the way that I would expect from the description of slices.


int main()
{
  char[] one = "abc\ndef\n";
  char[] two = one[0..3];
  printf( "%s\n", cast(char *) one );
  return( 0 );
}

generates as output

  abc
  def

instead of just
  abc



March 19, 2003
"Steve Adams" <adamss@ascinet.com> wrote in message news:b58ekm$1kqe$1@digitaldaemon.com...
> Can someone clear this up for me?  The following code doesn't work the way that I would expect from the description of slices.
>
>
> int main()
> {
>   char[] one = "abc\ndef\n";
>   char[] two = one[0..3];
>   printf( "%s\n", cast(char *) one );
>   return( 0 );
> }
>
> generates as output
>
>   abc
>   def
>
> instead of just
>   abc
>

Seems correct output to me...

Now it may have your expected output if you did:

printf("%s\n", cast(char *) two);

Am I missing something?

Later,
John


March 19, 2003
Sigh.  The printf line should be:

  printf( "%s\n", case(char *)two);


"Steve Adams" <adamss@ascinet.com> wrote in message news:b58ekm$1kqe$1@digitaldaemon.com...
> Can someone clear this up for me?  The following code doesn't work the way that I would expect from the description of slices.
>
>
> int main()
> {
>   char[] one = "abc\ndef\n";
>   char[] two = one[0..3];
>   printf( "%s\n", cast(char *) one );
>   return( 0 );
> }
>
> generates as output
>
>   abc
>   def
>
> instead of just
>   abc
>
>
>


March 19, 2003
:-)

"Steve Adams" <adamss@ascinet.com> wrote in message news:b58fm5$1lke$1@digitaldaemon.com...
> Sigh.  The printf line should be:
>
>   printf( "%s\n", case(char *)two);
>
>


March 19, 2003
Nothing like hitting send too soon.

But, it still doesn't work.  It always prints all the characters in one.


"John Reimer" <jjreimer@telus.net> wrote in message news:b58fs0$1lm4$1@digitaldaemon.com...
> :-)
>
> "Steve Adams" <adamss@ascinet.com> wrote in message news:b58fm5$1lke$1@digitaldaemon.com...
> > Sigh.  The printf line should be:
> >
> >   printf( "%s\n", case(char *)two);
> >
> >
>
>


March 19, 2003
Well, I think the reason is because you've taken a slice of array one and put it in two, and two doesn't have a null terminator in it's slice by default, so you end up printing until there's a null termination of the string which, it appears, is in the same address as the string termination of one..

I didn't realize, though, that a slice seems to be like a pointer to variable one's memory space, if indeed it has the output you say. Or maybe it copies the whole string, but only represents the slice.

A convoluted way of saying it, I'm sure... but I think that's the problem.

Later,
John


March 19, 2003
"Steve Adams" <adamss@ascinet.com> wrote in news:b58g2f$1lo7$1@digitaldaemon.com:

> Nothing like hitting send too soon.
> 
> But, it still doesn't work.  It always prints all the characters in one.
> 
> 
> "John Reimer" <jjreimer@telus.net> wrote in message news:b58fs0$1lm4$1@digitaldaemon.com...
>> :-)
>>
>> "Steve Adams" <adamss@ascinet.com> wrote in message news:b58fm5$1lke$1@digitaldaemon.com...
>> > Sigh.  The printf line should be:
>> >
>> >   printf( "%s\n", case(char *)two);

This is a very common mistake.  D strings are
not null terminated, except for the constant
ones defined inline.  Try this instead.

printf("%.*s\n",two);



March 19, 2003
Seems like it's a pointer and a count (since strings are counted).  Not a
bad model, until
you want to print it out.  I'm working around it with

  printf( "%.*s\n", two.length, (char *)two );

but that seems rather wasteful.  Either the cast should really do a case, or
there should be
a format specifier for the counted strings.

Might be a way to do this with an array copy too I suppose.


"John Reimer" <jjreimer@telus.net> wrote in message news:b58gmo$1m3a$1@digitaldaemon.com...
> Well, I think the reason is because you've taken a slice of array one and put it in two, and two doesn't have a null terminator in it's slice by default, so you end up printing until there's a null termination of the string which, it appears, is in the same address as the string termination of one..
>
> I didn't realize, though, that a slice seems to be like a pointer to variable one's memory space, if indeed it has the output you say. Or maybe it copies the whole string, but only represents the slice.
>
> A convoluted way of saying it, I'm sure... but I think that's the problem.
>
> Later,
> John
>
>


March 19, 2003
> This is a very common mistake.  D strings are
> not null terminated, except for the constant
> ones defined inline.  Try this instead.
>
> printf("%.*s\n",two);
>
>

Ah, now there's a good answer :-)



March 19, 2003
Thanks cleaner.  Thanks.


> >> news:b58fm5$1lke$1@digitaldaemon.com...
> >> > Sigh.  The printf line should be:
> >> >
> >> >   printf( "%s\n", case(char *)two);
>
> This is a very common mistake.  D strings are
> not null terminated, except for the constant
> ones defined inline.  Try this instead.
>
> printf("%.*s\n",two);
>
>
>


« First   ‹ Prev
1 2