views:

62

answers:

1

Hi,

after having no success with my question on How to use float ** in Python with Swig?, I started thinking that swig might not be the weapon of choice. I need bindings for some c functions. One of these functions takes a float**. What would you recomend? Ctypes?

Interface file:

extern int read_data(const char *file,int *n_,int *m_,float **data_,int **classes_);
A: 

I've used ctypes for several projects now and have been quite happy with the results. I don't think I've personally needed a pointer-to-pointer wrapper yet but, in theory, you should be able to do the following:

from ctypes import *

your_dll = cdll.LoadLibrary("your_dll.dll")

PFloat = POINTER(c_float)
PInt   = POINTER(c_int)

p_data    = PFloat()
p_classes = PInt()
buff      = create_string_buffer(1024)
n1        = c_int( 0 )
n2        = c_int( 0 )

ret = your_dll.read_data( buff, byref(n1), byref(n2), byref(p_data), byref(p_classes) )

print('Data:    ', p_data.contents)
print('Classes: ', p_classes.contents)
Rakis
@ Rakis, if you post this answer also to my other question 'How to use float ** in Python with Swig?' (see 'Linked' section), I'd accept your answer there as well;
Framester