Access to structures defined in C
Joe
jma at freedomcircle.com
Tue Sep 18 02:39:39 UTC 2018
On Sunday, 10 June 2018 at 17:59:12 UTC, Joe wrote:
> That worked but now I have a more convoluted case: a C array of
> pointers to int pointers, e.g.,
>
> int **xs[] = {x1, x2, 0};
> int *x1[] = {x1a, 0};
> int *x2[] = {x2a, x2b, 0};
> ...
> int x2a[] = { 1, 3, 5, 0};
>
> Only the first line is exposed (and without the
> initialization). So I tried:
>
> extern(C) __gshared extern int**[1] xs;
After a long hiatus, I'm back to working on something related to
the above, but now that various other C pieces have been
converted to D I'm down to converting these static arrays to D.
There are two arrays that are giving me trouble. The second type
is like that shown above. The first is a simpler array of
pointers to int, e.g.,
int *yp = {2, 4, 0};
int *yq = {10, 12, 0};
int *ys[] = {yp, yq, 0};
In D, I first declared these as
int[] yp = [2, 4];
int[] yq = [10, 12];
__gshared int*[] ys = [ &yp, &yq ];
The compiler (ldc2) gave errors like "cannot take address of
thread-local variable yp at compile time" or "static variable yp
cannot be read at compile time" (when I replaced the "&yp" by
"yp[0]". Eventually, I managed to get them to compile without
errors by using the following:
immutable int[] yp = [2, 4];
immutable int[] yq = [10, 12];
__gshared immutable(int[])[] ys = [ yp, yq ];
I still haven't tested them (or linked them) so I don't know what
other changes I'll have to make to the library (now in D) where
ys is declared as:
__gshared extern int*[] ys;
Presumably changing it to
__gshared extern immutable(int[])[] ys;
should do the trick (everything is still within extern (C))? But
I suspect I'll have to change several array access statements as
well.
However, I've been unable to compile the other case, which now
looks like this:
immutable int x1a[] = [ 9, 7, 5];
immutable(int[])[] x1 = [ x1a ];
immutable(int[])[] x2a = [ 1, 3, 5];
...
immutable(int[])[] x2 = [ x2a, x2b ];
__gshared immutable(int[])[][] xs = [ x1, x2 ];
The error is like "static variable x2a cannot be read at compile
time". It seems like I would have to wrap that immutable(int[])
within another immutable, as
immutable(immutable(int[])[]) x2 ...
and extend that to xs as well.
I'll try it later but I'd like some confirmation or better yet,
pointers to where this is explained in some comprehensible way,
i.e., on what can you apply an address operator or array
subscript and when, is the behavior differerent for immutable,
const, static, thread-local and why?
More information about the Digitalmars-d-learn
mailing list