Access to structures defined in C

Joe jma at freedomcircle.com
Sun Jun 10 17:59:12 UTC 2018


On Wednesday, 14 March 2018 at 02:17:57 UTC, Adam D. Ruppe wrote:
> The type system would *like* to know, certainly for correct 
> range errors, but if you declare it as the wrong length and use 
> the .ptr, it still works like it does in C:
>
> extern(C) __gshared extern char*[1] files; // still works
>
> import core.stdc.stdio;
>
> void main() {
>         printf("%s\n", files.ptr[2]); // ptr bypasses the range 
> check
> }


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;

The D compiler accepts that, but just about any manipulation gets 
screamed at, usually with Error: only one index allowed to index 
int. Note that I'm trying to access the ints, i.e., in C 
something like xs[1][0][2] to access the 5 in x2a. Do I have to 
mimic the intermediate C arrays?

> But if you can match the length, that's ideal.

Unfortunately, although the C array lengths are known at C 
compile time, they're not made available otherwise so I'm afraid 
the [1] trick will have to do for now.



More information about the Digitalmars-d-learn mailing list