char[] to string

Jonathan Sternberg jonathansternberg at gmail.com
Fri Jun 10 19:56:19 PDT 2011


Why doesn't this work?

import std.stdio;

string copy_string(char [] input)
{
    return input.dup;
}

int main()
{
    char [] buf = ['h', 'e', 'l', 'l', 'o'];
    writeln( copy_string(buf) );
}

I want to do something more complex. In my code, I want to have a dynamic
array that I can append stuff into and then return it as a string. In C++, a
non-const variable can be implicitly converted into a const. I know string is
an alias for const char. Is there a reason why it won't implicitly convert it?

I hesitate to use cast for this type of thing as it probably indicates I'm
doing something fundamentally wrong as I'm just starting to learn the language.


More information about the Digitalmars-d-learn mailing list