Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c结构到Python_Python_C_Ctypes - Fatal编程技术网

c结构到Python

c结构到Python,python,c,ctypes,Python,C,Ctypes,我有以下c代码: rx_data_t* readBuffer() { rx_data_t buffer[512]; results = doSomething() rx_data_t* newBuf = malloc(results.count()); for(i=0;i<results.count();i++) { newBuf[i].receive_time = buffer[i].receive_time;

我有以下c代码:

rx_data_t* readBuffer()
{
    rx_data_t buffer[512];

    results = doSomething()

    rx_data_t* newBuf = malloc(results.count());

    for(i=0;i<results.count();i++)
    {
        newBuf[i].receive_time = buffer[i].receive_time;
        newBuf[i].low = buffer[i].low;
        newBuf[i].high = buffer[i].high;
    }

    return newBuf;
}
这是可行的,我从c结构中得到了所有的结果,但是如果我遍历res,会有数百个结果,而不是我所期望的一两个

有什么我做错了还是没做


谢谢。

我自己没有这样做,所以我不想发布答案,但。。。python是否需要一个数据结构列表?是否需要在其中某个位置返回长度为1的值?另外,那些
self.lib
调用应该在哪里?根据缩进,它们不是类的一部分;该类只是定义
BufferResults
结构。
 class BufferResults(ctypes.Structure):
    """ creates a struct to match rx_data_t
    """

    _fields_ = [
        ('receive_time', ctypes.c_ulong),
        ('low', ctypes.c_ushort),
        ('high', ctypes.c_ushort)
    ]

self.lib.readBuffer.restype = ctypes.POINTER(BufferResults)

res = self.lib.readBuffer()