digitalmars.D.learn - Can't assign string to char * like the docs say
- Doctor J (15/15) May 13 2009 Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this d...
- Trass3r (3/13) May 13 2009 Of course this doesn't work.
- Steven Schveighoffer (12/30) May 13 2009 To make this more clear, the example text from the array page says:
- Gide Nwawudu (5/11) May 14 2009 It's a documentation bug, see.
- Jarrett Billingsley (8/16) May 13 2009 ent
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 crashTo 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, 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. -SteveIt's a documentation bug, see. http://d.puremagic.com/issues/show_bug.cgi?id=996 Gide
May 14 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 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