pass float array(list) parameter to C

Z

zaley

In my program, python is communicated with C use ctypes .
I can't find a better way to pass float array(list) parameter to C
function.
Can someone give me a help?
 
M

marek.rocki

You can also do it with ctypes only; too bad it's not really well
documented. c_float is a type of a floating point number and it has *
operator defined, so that c_float*4 is a type of a 4-element array of
those numbers. So if you want to construct an array of floats from a
list of floats, you can do it like this:

from ctypes import c_float

x = [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]
float_array_type = c_float*len(x)
float_array = float_array_type(*x)
print float_array
 
Z

zaley

You can also do it with ctypes only; too bad it's not really well
documented. c_float is a type of a floating point number and it has *
operator defined, so that c_float*4 is a type of a 4-element array of
those numbers. So if you want to construct an array of floats from a
list of floats, you can do it like this:

from ctypes import c_float

x = [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]
float_array_type = c_float*len(x)
float_array = float_array_type(*x)
print float_array

Is there has any difference between the following arrays $B!)(B

Buf0=(c_float* 9)(57,57,57,57,57,5,5,5,5)
Buf0=array('f', [57,57,57,57,57,5,5,5,5])

function called like this in python:
ret= SetAnalog(9,Buf0)

function prototype in C like this
int SetAnalog(UINT uChannel,float* pBuf)
{
.......
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top