I have the following c function into which I want to pass a dummy pointer so that I can retrieve the generated array:
int generateFloatArr(float **outputArr){
float result[4] = {1.0,2.0,3.0,4.0};
*outputArr=&result;
return 4;
}
With it, I can do the following in c:
float *output;
int arrLen = generateFloatArr(&output);
printf("%f \n",output[0]); //prints "1.000000"
With CFFI I am stuck interacting with this function.
I can create a pointer with:
p_floatArr=ffi.new("float *")
But I'm unsure how to get the address of p_floatArr
to pass into lib.generateFloatArr()
.
I tried this instead which the python-CFFI docs seem to suggest doing:
p_p_floatArr=ffi.new("float **")
lib.generateFloatArr(p_p_floatArr)
print(p_p_floatArr[0][0])
However the float value is incorrect. If I do the analog of the above in c:
float **output;
generateFloatArr(output);
printf("%f \n",output[0][0]);
This gives a segfault.
How can I properly pass a pointer (to a pointer) to this function?
within generateFloatArr()
, either:
*outputArr
needs to be allocated with malloc()
and then filled with values, or
result needs to be initialized as static const float result[4]={...}
From the python side, it works to do:
p_p_floatArr=ffi.new("float **")
lib.generateFloatArr(p_p_floatArr)
print(p_p_floatArr[0][0])