string to char*

Jonathan M Davis jmdavisprog at gmail.com
Sat Sep 11 23:02:44 PDT 2010


On Saturday 11 September 2010 09:07:38 bearophile wrote:
> I don't know why it returns a const(char)* instead of a char*. Do you know
> why?
> 
> Bye,
> bearophile

Well, if you look at toStringz()'s implementation, you may notice that there's 
commented out code which would not make a copy if there's a 0 in memory one 
passed the end of the string. It would simply use that 0 as the end of the const 
char* and avoid the copy. That being the case, it avoids a copy but must be 
const, because the string is immutable. Now, why that code is commented out, I 
don't know, and if toStringz() continues to always copy the string, then char* 
would likely be a better choice. But it could be that whatever issue made it so 
that the non-copying version was commented out will be fixed at some point, and 
toStringz() will once again cease to make a copy if it doesn't have to, at which 
point it would need to return const.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list