D array to void* and back
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 3 14:28:28 PDT 2015
On 08/03/2015 02:23 PM, ref2401 wrote:
> void funcLibC_Array(void* data) {
> int[] arr = *cast(int[]*)data;
You are still in D, so int[] has a different meaning from a C array.
(D's arrays are array-like. ;) )
Since arr.ptr is the pointer to the first element, and since that is
exactly how C functions access array elements, do this:
import std.stdio;
void funcLibC_Array(void* data) {
int* arr = cast(int*)data;
writeln("funcLibC_Array: ", arr);
foreach (i; 0 .. 3) {
writeln(arr[i]);
}
}
void main(string[] args) {
int[] arr = [1, 2, 3];
funcLibC_Array(arr.ptr); // 'Access Violation error' is thrown here.
}
But you still need to communicate how many elements there are in the
array. (I used literal 3).
Ali
More information about the Digitalmars-d-learn
mailing list