[Numpy-discussion] A question about ctypes and numpy

william ratcliff william.ratcliff@gmail....
Tue Jan 15 04:40:00 CST 2008

Hi!  I have been having some difficulty understanding how to deal with
multidimensional ndarrays in structs with ctypes.  Namely, I want to the
define numpy arrays in python and have them operated on in C, and have them
updated on the python side.  Here is a toy example:

c code fragment:
typedef struct {
  double X[4][4];
  double Y[4][4];
} matrices;  //there could be other elements in the struct between the
arrays, after them, etc.

int correct(matrices* pt){
 int i=2,j=1;


import numpy as N
import ctypes


class fiduc(ctypes.Structure):

foo = N.ctypeslib.load_library('foo.dll', '.')
print xdata
print ydata

My question is:
If I want to define a class in python and later to have my c code operate on
an instance of it, then I need to know the c_type representation for the
ndarray.  I can't simply blast the data into the structure because due to
memory alignment, structs in C are not guaranteed to have contiguous
elements in memory--I also don't want to take a risk with for example my
mingw generated dll having a different alignment for a structure than what a
ctypes made using msvc expects.   Thanks!!!!!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080115/ee08241f/attachment.html 

More information about the Numpy-discussion mailing list