D - Slicing on strings (char [ ])?
- Jon Thoroddsen (11/11) Dec 03 2003 int main( char [] [] args ) {
- J C Calvarese (12/26) Dec 03 2003 It's a gotcha.
- Jon Thoroddsen (15/41) Dec 04 2003 Ah, ok, thanks!
- J C Calvarese (11/43) Dec 04 2003 ...
- Vathix (13/63) Dec 04 2003 ingastring
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
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? NonniIt'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
In article <bqmats$1t17$1 digitaldaemon.com>, J C Calvarese says...Jon Thoroddsen wrote: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?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? NonniIt'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 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
Dec 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 )ingastringhttp://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#ErrorAccessViolationonprintchar[] 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? NonniIt'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...values intoHope this helps. JustinAh, ok, thanks! I think I was unconsciously expecting the slice operator to copy thepr, which would have resulted in either "Hello(random garbage)" or anaccessviolation. 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 tothat?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