ctypes, arrays and pointers

Richard Jones richardjones at optushome.com.au
Fri Oct 6 19:29:13 EDT 2006


Thomas Heller wrote:
> Richard Jones schrieb:
>>     row = image_data + width * components * y
>> 
>> but you can't add an int to a c_char_Array_2415600.
> 
> This feature request has now come up the second time, so I guess I will
> have to find a solution for it.  Someone suggested a byref_at(obj, offset)
> method which would return a kind of pointer to 'obj' plus offset, and even
> provided a patch for it (on the ctypes-users lists, maybe even in the
> ctypes or Python SF tracker). Unfortunately that was after the feature
> freeze for Python 2.5.

The following will do what I wanted:

    def ptr_add(ptr, offset):
        address = addressof(ptr.contents) + offset
        return pointer(type(ptr.contents).from_address(address))

Thanks to Alex Holkner for the tip.


    Richard




More information about the Python-list mailing list