May 17, 2004 Re: toString(int) bug and two possible solutions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | On Mon, 17 May 2004 00:26:24 -0400, Vathix wrote: >> What is annoying is the D compiler's failure to notice this as it does an implicit cast that does not actually convert the D string into a C string. > > A char* isn't necessarily a C string. When char[] converts to char* it just drops the length, like it does from int[] to int* etc. Technically you are correct. A C-string is a (char *) AND the set of characters pointed to is deemed terminated when a zero-value char is found. However a (char *) is also not a (char []) either. The first is a pointer to a char, and the second is structure whose first element is a 4-byte integer followed by zero or more char elements. I thought that when a D string (char []) is converted to a C string (char *) a new piece of RAM is allocated (length of D-string + 1) and the char elements are copied to the new RAM location and a zero-char is placed in the last RAM position. The new RAM location is returned (char *). I'd still like the compiler to tell us when its implicitly casting char[] to char* for us. Wouldn't it save many annoying buglets? Something like this should do the conversions too, IMO ... char *x; char[] y; . . . x = cast (char *)y; y = cast (char [])x; -- Derek 17/May/04 4:34:02 PM |
Copyright © 1999-2021 by the D Language Foundation