[cffi-devel] C struct in C struct ?

James Bielman jamesjb at jamesjb.com
Sat May 6 22:58:32 UTC 2006


James Bielman <jamesjb at jamesjb.com> writes:

> Frank Goenninger <fgoenninger at prion.de> writes:
>
>> I do habe C structs lile these:
>>
>> struct a_struct
>> {
>>     int a;
>> };
>>
>> struct b_struct
>> {
>>    int b;
>>    struct a_struct a;
>> };
>>
>>
>> How would I model b_struct using defcstruct ?
>
> It looks like what you would think:
>
> (defcstruct a-struct
>   (a :int))
>
> (defcstruct b-struct
>   (b :int)
>   (a a-struct))

I should add that another potentially confusing thing here is when you
access the A slot of a B-STRUCT, you get back a pointer to it, instead
of the A-STRUCT by value.  For example:

(foreign-slot-value b 'b-struct 'b) -> 100 ; an int
(foreign-slot-value b 'b-struct 'a) -> #<pointer ...> ; a pointer

Probably this DWIM-ish behavior was also a mistake, and you should use
FOREIGN-SLOT-POINTER to access the structure instead (which does work
now):

(foreign-slot-value b 'b-struct 'a) -> error!
(foreign-slot-pointer b 'b-struct 'a) -> #<pointer ...>

James



More information about the cffi-devel mailing list