Haven't really worked with ctypes yet, but what about:
from ctypes import *
l = cdll.LoadLibrary("lib.so")
my_func = l.my_func
my_func.restype = POINTER(c_int)
i = 0;
rv = my_func()
while rv[i]:
print rv[i]
i += 1
Вопрос
From python I am trying to use c function that returns dynamically allocated zero terminated array of integers:
int* my_func(void)
{
int i;
int *ret = (int*)malloc((LEN + 1) * sizeof(int));
for (i = 0; i < LEN; i++) ret[i] = 1;
ret[LEN] = 0
return ret;
}
I need something like
from ctypes import *
l = cdll.LoadLibrary("lib.so")
my_func = l.my_func
my_func.restype = c_int * LEN
for x in my_func(): print x
The problem is that LEN
is unknown in python code, I need to read the array till first zero element.
Решение
Haven't really worked with ctypes yet, but what about:
from ctypes import *
l = cdll.LoadLibrary("lib.so")
my_func = l.my_func
my_func.restype = POINTER(c_int)
i = 0;
rv = my_func()
while rv[i]:
print rv[i]
i += 1