ctypes: reference of a struct member?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri May 1 23:56:50 EDT 2009


En Sat, 02 May 2009 00:51:29 -0300, Gabriel Genellina  
<gagsl-py2 at yahoo.com.ar> escribió:
> En Fri, 01 May 2009 14:39:39 -0300, ma <mabdelkader at gmail.com> escribió:
>
>> If I have this struct in C:
>>
>> struct spam {
>>      int ham;
>>   char foo;
>> };
>>
>>
>> if I have this declaration:
>> struct spam s_;
>>
>> If I wanted to pass a reference to a function of s_'s foo character, I
>> can do something like this:
>>
>> somefunc(&s_.foo)
>>
>> How do I do the same thing in ctypes?
>> ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1)
>
> ctypes.addressof(s_) + spam.foo.offset

Another way: ctypes from Python 2.6 lets you say:

byref(s_, spam.foo.offset)

-- 
Gabriel Genellina




More information about the Python-list mailing list