On 07/20/2010 02:23 PM, awishformore wrote:
When thinking about it, it makes sense to have string literals null
terminated in order to have C functions work with them. However, I
wonder about some stuff, for instance:
string s = "string";
// is s == "string\0" now?
No, but the byte after the string (in the executable) is 0.
char[] c = cast(char[])s;
// is c[6] == '\0' now?
No, c.length == 6.
char* p = s.ptr;
// is *(p+6) == '\0' now?
Yes. Since it came from a literal.
I think use of the zero terminator should be consistent. Either make
every string (and char[] for that matter) zero terminated in the
underlying memory for backwards compatibility with C or leave it to the
user in all cases.
/Max
The current situation allows for this:
printf("Hello!\n");
but not this:
string s = "Hello!\n";
printf(s);
Since the compiler knows about literals being null terminated, but not
just any string. I think it's a good compromise.