Thread overview
Slicing on strings (char [ ])?
Dec 04, 2003
Jon Thoroddsen
Dec 04, 2003
J C Calvarese
Dec 04, 2003
Jon Thoroddsen
Dec 04, 2003
J C Calvarese
Dec 04, 2003
Vathix
December 04, 2003
int main( char [] [] args ) {
char[] pr = "Hello Jon Thoroddsen !"[0..5];
printf(pr);
return 1;
}

this prints out
Hello Jon Thoroddsen !
rather than
Hello

is this a bug or a gotcha?

Nonni


December 04, 2003
Jon Thoroddsen wrote:

> int main( char [] [] args ) { char[] pr = "Hello Jon Thoroddsen !"[0..5];
> printf(pr);
> return 1;
> }
> 
> this prints out Hello Jon Thoroddsen !
> rather than
> Hello
> 
> is this a bug or a gotcha?
> 
> Nonni

It's a gotcha.

Try:
printf(pr ~ \0);

Basically what's happening is the printf function used is from the C Runtime Library.  It expects a null character to end the string. Concatenated a null character is probably the easiest way to get the desired result.

More info available here...
http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#ErrorAccessViolationonprintingastring

Hope this helps.

Justin

December 04, 2003
In article <bqmats$1t17$1@digitaldaemon.com>, J C Calvarese says...
>
>Jon Thoroddsen wrote:
>
>> int main( char [] [] args ) {
>> char[] pr = "Hello Jon Thoroddsen !"[0..5];
>> printf(pr);
>> return 1;
>> }
>> 
>> this prints out
>> Hello Jon Thoroddsen !
>> rather than
>> Hello
>> 
>> is this a bug or a gotcha?
>> 
>> Nonni
>
>It's a gotcha.
>
>Try:
>printf(pr ~ \0);
>
>Basically what's happening is the printf function used is from the C Runtime Library.  It expects a null character to end the string. Concatenated a null character is probably the easiest way to get the desired result.
>
>More info available here... http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#ErrorAccessViolationonprintingastring
>
>Hope this helps.
>
>Justin
>

Ah, ok, thanks!
I think I was unconsciously expecting the slice operator to copy the values into
pr, which would have resulted in either "Hello(random garbage)" or an access
violation.

So if I understand correctly:
char[] str = "yadayada";
char[] pr = str[4..6];

means that internally
pr.pointer = str.pointer + 4
pr.length = 6 - 4

rather than pr allocating it's own space, copying into it and pointing to that? So what happens here?

pr ~= "da";

Does pr then allocate it's own space? Isn't that a bit confusing?




December 04, 2003
Jon Thoroddsen wrote:
> In article <bqmats$1t17$1@digitaldaemon.com>, J C Calvarese says...
>>Jon Thoroddsen wrote:
>>>int main( char [] [] args ) { char[] pr = "Hello Jon Thoroddsen !"[0..5];
>>>printf(pr);
>>>return 1;
>>>}
...
>>
>>Try:
>>printf(pr ~ \0);
>>
...
> 
> Ah, ok, thanks!
> I think I was unconsciously expecting the slice operator to copy the values into
> pr, which would have resulted in either "Hello(random garbage)" or an access
> violation.
> 
> So if I understand correctly:
> char[] str = "yadayada";
> char[] pr = str[4..6];
> 
> means that internally pr.pointer = str.pointer + 4
> pr.length = 6 - 4
> 
> rather than pr allocating it's own space, copying into it and pointing to that?
> So what happens here?
> 
> pr ~= "da";
> 
> Does pr then allocate it's own space? Isn't that a bit confusing? 
> 
It is a bit confusing.  (I generally try not to worry about the details behind the scene -- as long as it works as expected.)

I don't claim to be an expert on this topic, but it seems that arrays are only copied if they are changed.

The nitty-gritty details seem to be here: http://www.digitalmars.com/d/memory.html
http://www.digitalmars.com/d/model.html

Justin

December 04, 2003
"Jon Thoroddsen" <Jon_member@pathlink.com> wrote in message news:bqn1el$2uhe$1@digitaldaemon.com...
> In article <bqmats$1t17$1@digitaldaemon.com>, J C Calvarese says...
> >
> >Jon Thoroddsen wrote:
> >
> >> int main( char [] [] args )

> >> char[] pr = "Hello Jon Thoroddsen !"[0..5];
> >> printf(pr);
> >> return 1;
> >> }
> >>
> >> this prints out
> >> Hello Jon Thoroddsen !
> >> rather than
> >> Hello
> >>
> >> is this a bug or a gotcha?
> >>
> >> Nonni
> >
> >It's a gotcha.
> >
> >Try:
> >printf(pr ~ \0);
> >
> >Basically what's happening is the printf function used is from the C Runtime Library.  It expects a null character to end the string. Concatenated a null character is probably the easiest way to get the desired result.
> >
> >More info available here...
>
>http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#ErrorAccessViolationonprint
ingastring
> >
> >Hope this helps.
> >
> >Justin
> >
>
> Ah, ok, thanks!
> I think I was unconsciously expecting the slice operator to copy the
values into
> pr, which would have resulted in either "Hello(random garbage)" or an
access
> violation.
>
> So if I understand correctly:
> char[] str = "yadayada";
> char[] pr = str[4..6];
>
> means that internally
> pr.pointer = str.pointer + 4
> pr.length = 6 - 4
>
> rather than pr allocating it's own space, copying into it and pointing to
that?
> So what happens here?
>
> pr ~= "da";
>
> Does pr then allocate it's own space? Isn't that a bit confusing?
>


From the page www.digitalmars.com/d/arrays.html it says :

 Concatenation always creates a copy of its operands, even if one of the
operands is a 0 length array, so:
 a = b   a refers to b
 a = b ~ c[0..0]  a refers to a copy of b

By the way, because dynamic arrays are slices, b is exactly the same as b[0 .. b.length]