[Numpy-discussion] Getting a numpy array from a ctype pointer

Travis E. Oliphant oliphant@enthought....
Thu Sep 4 15:01:17 CDT 2008


Paulo J. S. Silva wrote:
> Hello,
>
> I am writing some code interfacing C and Python using ctypes. In a
> callback function (in Python) I get in a parameter x which is c_double
> pointer and a parameter n which is c_int representing the length of the
> array. 
>
> How can I transform this information into a numpy array?
>
>   
Something like this may work:

from numpy import ctypeslib

r = ctypeslib.as_array(x._type_ * n)

If that doesn't work, then you can create an array from an arbitrary 
buffer or any object that exposes the __array_interface__ attribute.   
So, if you can get the address of c_double then you can use it as the 
data for the ndarray.

Ask if you need more help.

-Travis



More information about the Numpy-discussion mailing list