Passing string array to C

Andre Pany andre at
Thu Sep 10 14:31:41 UTC 2020


I have this coding. Function `sample` will later be called from C
and should provide access to a string array.

I tried to read the string values after the function call
and I can access the first string, but for the second string,
there is an access violation.

Why does it crash?

Kind regards

import std;

void main()
	size_t* i;
	const(wchar)* r;
	sample(&r, &i);

         // Try to read the 2 string values
	const(wchar) ** r2;
	r2 = &r;
	auto arr = r2[0..*i];
	writeln(to!string(arr[0])); // Works
	writeln(to!string(arr[1])); // Fails

extern(C) export void sample(const(wchar)** r, size_t** c)
	string[] arr = ["fooä", "bar"];
	auto z = new const(wchar)*[arr.length];
	foreach(i, ref p; z)
		p = toUTF16z(arr[i]);
         *r = z[0];
	*c = new size_t();
	**c = arr.length;

More information about the Digitalmars-d-learn mailing list