Thread overview
Can't assign string to char * like the docs say
May 13, 2009
Doctor J
May 13, 2009
Trass3r
May 14, 2009
Gide Nwawudu
May 13, 2009
Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile:

    void main()
    {
        string str = "abc";
        char* p = str;		// pointer to 1st element
    }

"Error: cannot implicitly convert expression (str) of type char[] to char*"

I agree it shouldn't compile; I guess I'm asking why the docs say it does.

While I'm at it, what's up with the very first strings example:

    char[] str;
    char[] str1 = "abc";
    str[0] = 'b';        // error, "abc" is read only, may crash

Should that just be:

    char[] str = "abc";
    str[0] = 'b';        // error, "abc" is read only, may crash


May 13, 2009
Doctor J schrieb:
> Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile:
> 
>     void main()
>     {
>         string str = "abc";
>         char* p = str;		// pointer to 1st element
>     }
> 
> "Error: cannot implicitly convert expression (str) of type char[] to char*"
> 

Of course this doesn't work.
Try char* p = str.ptr;
May 13, 2009
On Wed, 13 May 2009 14:28:46 -0400, Doctor J <nobody@nowhere.com> wrote:

> Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile:
>
>     void main()
>     {
>         string str = "abc";
>         char* p = str;		// pointer to 1st element
>     }
>
> "Error: cannot implicitly convert expression (str) of type char[] to char*"
>
> I agree it shouldn't compile; I guess I'm asking why the docs say it does.
>
> While I'm at it, what's up with the very first strings example:
>
>     char[] str;
>     char[] str1 = "abc";
>     str[0] = 'b';        // error, "abc" is read only, may crash
>
> Should that just be:
>
>     char[] str = "abc";
>     str[0] = 'b';        // error, "abc" is read only, may crash
>
>


To make this more clear, the example text from the array page says:

A pointer to a char can be generated:
char* p = &str[3];	// pointer to 4th element
char* p = str;		// pointer to 1st element

where str is previously identified as a string (i.e. char[])

it is a documentation bug, this behavior is not allowed.  Please submit a bug to bugzilla: http://d.puremagic.com/issues/

On the other hand, string *literals* are implicitly castable to char *:

char *p = "abc";

works.

-Steve
May 13, 2009
On Wed, May 13, 2009 at 2:28 PM, Doctor J <nobody@nowhere.com> wrote:
> Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile:
>
>    void main()
>    {
>        string str = "abc";
>        char* p = str;          // pointer to 1st element
>    }
>
> "Error: cannot implicitly convert expression (str) of type char[] to char*"
>
> I agree it shouldn't compile; I guess I'm asking why the docs say it does.

Once upon a time, it used to.  But the implicit conversion from T[] to T* was removed when it was deemed to be too easy to make mistakes using it.
May 14, 2009
On Wed, 13 May 2009 16:42:51 -0400, "Steven Schveighoffer" <schveiguy@yahoo.com> wrote:

>it is a documentation bug, this behavior is not allowed.  Please submit a bug to bugzilla: http://d.puremagic.com/issues/
>
>On the other hand, string *literals* are implicitly castable to char *:
>
>char *p = "abc";
>
>works.
>
>-Steve

It's a documentation bug, see. http://d.puremagic.com/issues/show_bug.cgi?id=996

Gide