On 05/16/2014 08:15 AM, Andrew Brown wrote: > I guess my confusion came about because in the page about interfacing > with C, there's a static array example where parameters are given in > terms D understands: > > extern (C) > { > void foo(ref int[3] a); // D prototype > } > > I guess D has no problem translating that into a simple pointer that C > can deal with. I assumed the same would be true of dynamic arrays, but > maybe the leap is too far?
There is a major difference. A static array is direct equivalent of C arrays when it comes to how they are stored in memory. Static arrays are simply consecutive elements. (Of course, static arrays are superior to C arrays in many other aspects. :))
One difference between C arrays is the fact that static arrays are by-value when passed even to functions. (No more "decaying to pointer to first element" confusion.)
Since we know that references are implemented as pointers, 'ref int[3] a' is passed as a.ptr. Since the memory layout is the same as a C array, it works perfectly.
On the other hand, dynamic arrays (aka slices) are the equivalent of the following struct:
struct Slice(T) { size_t length; T * ptr; // points to an array of elements } Ali