I know there are often many ways to solve certain problems. But here I know how I want to get it, but I can't get it to work with Python and SWIG ...
I have a C function that returns me an array of double values:
double *my(int x) { double a,b,*buf; buf = malloc (x * sizeof(double)); a=3.14; b=2.7; buf[0]=a; buf[1]=b; return buf; }
Here I finally want to have an array as a return value. No, as in many examples, the "void" function, which is written to the input array. Now I would like to get a SWIG-python shell that can be used as:
>>> import example >>> print example.my(7) [3.14,2.7]
Whatever I do, I have some conceptual issues here - I always get s.th. e.g. <Swig Object of type 'double *' at 0xFABCABA12>
I tried to define some typemaps in my swg file:
%typemap(out) double [ANY] { int i; $result = PyList_New($1_dim0); for (i = 0; i < $1_dim0; i++) { PyObject *o = PyFloat_FromDouble((double) $1[i]); PyList_SetItem($result,i,o); } }
But still I can not get my results as needed. Does anyone have a simple code example to achieve this?
c python arrays return swig
user701370
source share