tags:

views:

56

answers:

1

Hi there,

I read the official ctypes tutorial and also searched SO, but I could not find a way to declare this kind of structure with ctypes. This structure is returned by one of the functions I write an Python interface for.

typedef struct{
    int i;
    float *b1;
    float (*w1)[];
}foo;

This is what I have so far:

class foo(Structure):
 _fields_=[("i",c_int),
  ("b1",POINTER(c_int)),
  ("w1",?????????)]

Thanks for your help!

+1  A: 

In C, a pointer to an array stores the same memory address as a pointer to the first element in the array. Therefore:

class foo(Structure):
    _fields_=[("i",c_int),
              ("b1",POINTER(c_int)),
              ("w1",POINTER(c_float))]

You can access the elements of the array using integer indexes. For example: myfoo.w1[5].

It would be better C coding style to declare w1 as float *w1, so that you can access elements of the array using myfoo->w1[5] instead of having to dereference twice.

Daniel Stutzbach
Hi Daniel,I dereference 'w1' twice as I use it as a 2d array.
Framester