Justin Johansson wrote:
Daniel Keep Wrote:

Big difference if you pass char[] variable .ptr to a C function.

In general, if you pass a string to a C function you should send it
through toStringz first.  If you don't, you're just begging for segfaults.

Agreed .. fair enough.

Actually I'm more interested in the semantics for default initialized char[].
Does it have exactly the same semantics as an empty string (in general D or 
runtime library, Phobos et. al. context)?

It isn't the same semantics:
a null array is {0, null}, while an empty array is {0, &zero} where zero is of type 'char zero = 0;' since string literals are zero terminated.

Their usage is mostly the same, you can concatenate both of them, append to both of them, and etc, all giving the same results. Where it makes a difference is when you need to enforce an invariant that .ptr is not null. Calling toStringz on either will give the same C string: a pointer to a zero value.

You have to remember that arrays are reference types; they are perfectly valid without referenced data. Think of pointers or objects for example, which are also reference types. Besides, if you initialize character arrays to "", what do you initialize other arrays to, and other reference types to? It just wouldn't be consistent.

Reply via email to