[Numpy-discussion] numpy int64 arrays and ctypes

JJ josh8912@yahoo....
Thu Mar 26 13:08:10 CDT 2009


Hello:
I hope someone can give me a tip on how to solve this simple problem. I use Ubuntu 8.10 64 bit and want to pass a numpy integer array to a shared library C program. For some reason, my C program is not able to read the info passed in the integer array, but can read from a passed double array. It is probably a simple fix, but I do not know what it is. I have the same problem using Python 2.5 and Python 2.6

In the python program, I have something like:

myInts = array([0,2,5,7]) #(of type 64 bit integer)
myDoub= random.rand(100)

myLib_ = ctypes.cdll['./mylib.so']
myfun = _myLib.myfun

p_myInts = ndpointer(myInts.dtype, myInts.ndim, myInts.shape, "C_CONTIGUOUS")
p_myDoub = ndpointer(myDoub.dtype, myDoub.ndim, myDoub.shape, "C_CONTIGUOUS")

myfun.argtypes = [p_myInts, p_myDoub]
myfun.restype = ctypes.c_int

result = myfun(myInts, myDoub)

Then in the header file I have:
int myfun(int *myInts double *myDoub);

Finally, in the C program I have:

int myfun(int *myInts double *myDoub){
int i;
for (i=0;, i<4; i++){
printf("myInt= %d, myDoub[myInt]= %g\n", myInt[i], myDoub[myInt[i]]);
}
}

This does not work, the integer array is not passed correctly, but the double array is. Neither %d or %Int64 work for the printf statement. Any suggestions?
John 


      


More information about the Numpy-discussion mailing list