Accessing the contents of an array variable using ctypes

I am using ctypes to access a file reading a C function in python. Since the data read is huge and unknown in size, I use **floatin C.

int read_file(const char *file,int *n_,int *m_,float **data_) {...}

Functions A mallocs2d array, called the datacorresponding size, is here nand m, and copies the values ​​to reference. See the following snippet:

*data_ = data;
*n_ = n;
*m_ = m;

I am accessing this function with the following python code:

p_data=POINTER(c_float)
n=c_int(0)
m=c_int(0)
filename='datasets/usps'
read_file(filename,byref(n),byref(m),byref(p_data))

Then I try to use p_datawith contents, but I get only one float value.

p_data.contents
c_float(-1.0)

My question is: how can I access datain python?

What did you recommend? Please feel free to indicate if I left something incomprehensible!

+5
1

python struct. ctypes ( , ):

#include <malloc.h>
void floatarr(int* n, float** f)
{
    int i;
    float* f2 = malloc(sizeof(float)*10);
    n[0] = 10;
    for (i=0;i<10;i++)
    { f2[i] = (i+1)/2.0; }
    f[0] = f2;
}

python:

from ctypes import *

fd = cdll.LoadLibrary('float.dll')
fd.floatarr.argtypes = [POINTER(c_int),POINTER(POINTER(c_float))]

fpp = POINTER(c_float)()
ip = c_int(0)
fd.floatarr(pointer(ip),pointer(fpp))
print ip
print fpp[0]
print fpp[1]

, capitals POINTER POINTER . byref POINTER, . POINTER , , .

+3

All Articles