【发布时间】:2012-07-08 14:49:31
【问题描述】:
我在 Python 代码和 C 代码中都有结构。我填写这些字段
("bones_pos_vect",((c_float*4)*30)),
("bones_rot_quat",((c_float*4)*30))
在具有正确值的 python 代码中,但是当我在 C 代码中请求它们时,我从所有数组单元中只得到 0.0。为什么我会失去价值观?我的结构的所有其他字段都可以正常工作。
class SceneObject(Structure):
_fields_ = [("x_coord", c_float),
("y_coord", c_float),
("z_coord", c_float),
("x_angle", c_float),
("y_angle", c_float),
("z_angle", c_float),
("indexes_count", c_int),
("vertices_buffer", c_uint),
("indexes_buffer", c_uint),
("texture_buffer", c_uint),
("bones_pos_vect",((c_float*4)*30)),
("bones_rot_quat",((c_float*4)*30))]
typedef struct
{
float x_coord;
float y_coord;
float z_coord;
float x_angle;
float y_angle;
float z_angle;
int indexes_count;
unsigned int vertices_buffer;
unsigned int indexes_buffer;
unsigned int texture_buffer;
float bones_pos_vect[30][4];
float bones_rot_quat[30][4];
} SceneObject;
【问题讨论】:
-
多维数组在 c 的内存中不是一对一映射的。因此 float[30][4] 实际上是一个浮点指针数组(大小=30)(指向浮点数组的开头)。 (c_float*4)*30) 可能实际上是一个数组数组(前 4 个浮点数,然后是第二个 4 个浮点数等)。你应该测试一下。
-
所以,我必须简单地发送 (c_float*(4*30))?
-
不,我认为应该是 POINTER(c_float)*30;然后使用
[i]访问单个值。例如pbase = bones_pos_vect[17];p = pbase[3] #third float in array;value = p.contents -
不,它不起作用,我只得到 0.0。另外,我尝试使用 (c_float*(4*30) 并得到相同的结果(((我想哭,因为它不起作用
-
我想将值发送到 C 代码
标签: python multidimensional-array python-3.x structure ctypes