Access to structures defined in C

Atila Neves atila.neves at gmail.com
Tue Sep 18 13:47:50 UTC 2018


On Tuesday, 18 September 2018 at 02:39:39 UTC, Joe wrote:
> 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};

This is valid C in the sense that it compiles, but I doubt it 
does what you think it does. This is equivalent code:

int *yp = 2;
int *yq = 10;

> int *ys[] = {yp, yq, 0};

This isn't even valid C code.

> In D, I first declared these as
>
> int[] yp = [2, 4];
> int[] yq = [10, 12];
> __gshared int*[] ys = [ &yp, &yq ];

D dynamic arrays are not equivalent to C arrays.


It's hard to see what you're trying to do with the code you 
posted. Have you tried instead to use a tool to translate the C 
headers?


More information about the Digitalmars-d-learn mailing list