Python结构中的动态数组和结构 [英] Dynamic arrays and structures in structures in python

查看:132
本文介绍了Python结构中的动态数组和结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ctypes在python中实现此C结构:

I am trying to implement this C structures in python using ctypes:

struct _rows {
    int cols_count;
    char *cols[];
}

struct _unit {
    int rows_count;
    struct _rows *rows;
}

int my_func(struct _unit *param);

问题是_rows.cols是动态大小的char指针数组,而_unit.rows是动态的_rows结构的大小数组。如何在python中使用ctypes实现此功能?

Problem is that _rows.cols is a dynamically sized array of char pointer and _unit.rows is a dynamically sized array of _rows structure. How can I implement this using ctypes in python?

我能够定义一个函数,该函数将返回具有可变数量的char指针的_rows结构:

I was able to define a function that will return a _rows structure with variable number of char pointers:

def get_row(cols):
    class Row(ctypes.Structure):
        _fields_ = [("cols_count", ctypes.c_int), 
                    ("cols", ctypes.c_char_p * cols)
                   ]

我不知道该怎么办做nex,这有点模糊,ctypes文档也无济于事。

I don't know what to do nex, it's all a bit fuzzy and ctypes documentation isn't helping.

推荐答案

我正在对OP想要,如果有更简单的方法可以做到,我会建议您,但这就是我想出的:

I'm making some assumptions about what the OP wants, and I'd love suggestions if there is an easier way to do this, but this is what I came up with:

import string
from ctypes import Structure,c_int,c_char_p,POINTER,cast,pointer,byref,CDLL

class Row(Structure):
    _fields_ = [('cols_count', c_int), 
                ('cols', POINTER(c_char_p))]
    def __init__(self,cols):
        self.cols_count = cols
        # Allocate an array of character pointers
        pc = (c_char_p * cols)()
        self.cols = cast(pc,POINTER(c_char_p))            

class Unit(Structure):
    _fields_ = [('rows_count', c_int),
                ('rows',POINTER(Row))]
    def __init__(self,rows,cols):
        self.rows_count = rows
        # Allocate an array of Row structures.
        # This does NOT call __init__.
        pr = (Row * rows)()
        # Call init manually with the column size.
        for r in pr:
            r.__init__(cols)
        self.rows = cast(pr,POINTER(Row))

unit = Unit(2,3)

# Stuff some strings ('aaaaa','bbbbb',etc.)
for i in xrange(unit.rows_count):
    for j in xrange(unit.rows[i].cols_count):
        unit.rows[i].cols[j] = string.ascii_lowercase[i*5+j]*5

dll = CDLL('test.dll')
dll.my_func(byref(unit))



test.c



test.c

#include <stdio.h>

struct _rows {
    int cols_count;
    char **cols;
};

struct _unit {
    int rows_count;
    struct _rows *rows;
};

__declspec(dllexport) int my_func(struct _unit *param)
{
    int i,j;
    for(i=0;i<param->rows_count;i++)
        for(j=0;j<param->rows[i].cols_count;j++)
            printf("%d,%d = %s\n",i,j,param->rows[i].cols[j]);
    return 0;
}



makefile



与Visual Studio 2010一起编译。

makefile

Compiled with Visual Studio 2010.

test.dll: test.c
    cl /W4 /LD test.c



输出



Output

0,0 = aaaaa
0,1 = bbbbb
0,2 = ccccc
1,0 = fffff
1,1 = ggggg
1,2 = hhhhh

这篇关于Python结构中的动态数组和结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆