multi-dimension array in ctypes
Monday, 29. May 2006, 11:03:12
I want to define an integer array x(5,3) (fortran style declaration), then corresponding python code is
from ctypes import * xarray = c_int * 3 * 5 x = xarray()
because ctypes parse the definition as (c_int*3)*5, so first dimension 5, and second 3. the order is reversed. it's confusing. Thomas Helle has given a function to make it clearer. see the code listed below.
from ctypes import *
def create_array_type(type, dimensions):
for d in dimensions[::-1]:
print d
type = type*d
return type
# array 2 dimension (5,3)
xarray = create_array_type(c_int, (5,3))
x = xarray()








