On Friday, 31 August 2018 at 17:50:17 UTC, Steven Schveighoffer
wrote:
...
When you use const char* in D, it's expecting a *pointer* to be
stored at that address, not the data itself. So using it means
segfault. The static array is the correct translation, even
though it leaks implementation details.
In C, it's working because C has the notion of a symbol being
where an array starts. D has no concept of a C array like that,
every array must have a length. So there is no equivalent you
can use in D -- you have to supply the length.
I think this is only correct for dynamic arrays. For static
arrays I have the impression that it works exactly as in C, i.e.
the address of the array is the address of the first array
element. See this simple code:
import std.stdio;
import std.array;
void main()
{
// static array
ulong [4] ulArr1 = [0,1,2,3];
ulong *p1 = ulArr1.ptr;
ulong *p2 = &(ulArr1[0]);
ulong [4] *p3 = &ulArr1;
writeln("same pointers: ", cast(void *)p1 == cast(void *)p2);
writeln("same pointers: ", cast(void *)p3 == cast(void *)p2);
writeln("");
// dynamic array
ulong [] ulArr2 = [0,1,2,3];
p1 = ulArr2.ptr;
p2 = &(ulArr2[0]);
ulong [] *p5 = &ulArr2;
writeln("same pointers: ", cast(void *)p1 == cast(void *)p2);
writeln("same pointers: ", cast(void *)p5 == cast(void *)p2);
} // end main()
This produces (with dmd):
same pointers: true
same pointers: true
same pointers: true
same pointers: false