Re: Translating C "static arrays" into D?

ketmar ketmar at ketmar.no-ip.org
Mon Feb 26 18:07:11 UTC 2018


H. S. Teoh wrote:

> What's the correct translation of the following C declarations into D?
>
> 	typedef double[1] mytype;
>
> 	void someFunc(mytype x, mytype *y, mytype **z);
>
> 	struct SomeStruct {
> 		mytype x;
> 		mytype *y;
> 		mytype **z;
> 	}
>
> I need this to interface with an external C library.  Currently, I just
> wrapped the above as-is inside an extern(C) block.  But I suspect it may
> be wrong, because:
>
> 1) In C, declaring a function parameter of type double[1] is, IIRC, the
> same thing as declaring it as double*.  But in D, double[1] passes one
> double by value as a static array. So there may be an API mismatch here.
>
> 2) In C, declaring a *variable* or struct field as double[1] has
> essentially the same semantics as D's static arrrays.  Meaning that I
> cannot just change the declaration of mytype in order to get the correct
> behaviour of function parameters.
>
> 3) I'm getting a segfault at runtime of some C++ code into D, that calls
> the library via this C API, and I suspect it's probably due to (1).
>
>
> T

in C, arrays are *always* decaying to pointers. so

	void foo (int x[2])

is the same as

	void foo (int* x)

`[2]` is purely informational.

that is, in D it will be:

	alias mytype = double*;


More information about the Digitalmars-d mailing list