C API: array of floats/ints from python to C and back

Scott David Daniels Scott.Daniels at Acm.Org
Sat Dec 27 17:12:29 EST 2008


Daniel Fetchinson wrote:
> I'm trying to write an extension module in C which contains a single
> function with the following prototype:
>     void func( int N, int * arg1, int * arg2, int * ret );
> Here arg1 and arg2 are length N arrays, and the function computes ret
> which is also an N length array. From python I'd like to call this
> function as
> ret = func( [ 1, 2, 3 ], [ 2, 3, 4] )

This requirement pretty much dictates the slow answer you have.

 > Does this mean that I can only pass the arrays from python to C as
 > generic python objects and in a later operation I need to get the
 > elements from this generic python object, construct a C array and pass
 > that to the C function? ... What's the simplest way of doing this?

Either use ctypes, look into array.array, or look into numpy.array.

I'd just use numpy, myself:
     import numpy
     total = numpy.array([1, 2, 3]) + numpy.array([2, 3, 4])

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list