www.digitalmars.com         C & C++   DMDScript  

D - Slicing on strings (char [ ])?

reply Jon Thoroddsen <Jon_member pathlink.com> writes:
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
Dec 03 2003
parent reply J C Calvarese <jcc7 cox.net> writes:
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
Dec 03 2003
parent reply Jon Thoroddsen <Jon_member pathlink.com> writes:
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?
Dec 04 2003
next sibling parent J C Calvarese <jcc7 cox.net> writes:
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? 
 

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
Dec 04 2003
prev sibling parent "Vathix" <vathix dprogramming.com> writes:
"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

Hope this helps.

Justin

Ah, ok, thanks! I think I was unconsciously expecting the slice operator to copy the

 pr, which would have resulted in either "Hello(random garbage)" or an

 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

 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]
Dec 04 2003