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);
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?
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)
]
I don’t know what to do nex, it’s all a bit fuzzy and ctypes documentation isn’t helping.
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:
demo.py
test.c
makefile
Compiled with Visual Studio 2010.
Output