www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't assign string to char * like the docs say

reply Doctor J <nobody nowhere.com> writes:
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
next sibling parent Trass3r <mrmocool gmx.de> writes:
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
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
parent Gide Nwawudu <gide btinternet.com> writes:
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
May 14 2009
prev sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
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 do=
esn't compile:
 =A0 =A0void main()
 =A0 =A0{
 =A0 =A0 =A0 =A0string str =3D "abc";
 =A0 =A0 =A0 =A0char* p =3D str; =A0 =A0 =A0 =A0 =A0// pointer to 1st elem=
ent
 =A0 =A0}

 "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 13 2009