Translating C "static arrays" into D?

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 26 18:33:58 UTC 2018


On 2/26/18 12:54 PM, 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.

If you declare mytype as:

alias mytype = double[1];

Then you can use it pretty much anywhere. The only exception is when 
it's the exact type of a parameter. In that case, use ref:

extern(C) void someFunc(ref mytype x, mytype *y, mytype **z);

-Steve


More information about the Digitalmars-d mailing list