I have encountered the following strange behaviour of ctypes. When I convert a numpy array via ctypes to an int pointer some values are lost and additional zeros are added. More specifically, when I convert a numpy array with the following code
from numpy import *
import ctypes
from numpy.ctypeslib import ndpointer
x = arange(1, 10)
xInt = x.ctypes.data_as(ctypes.POINTER(ctypes.c_int * len(x)))
xLong = x.ctypes.data_as(ctypes.POINTER(ctypes.c_long * len(x)))
Then I get the following confusing results:
print [i for i in xInt.contents]
[1, 0, 2, 0, 3, 0, 4, 0, 5]
print [i for i in xLong.contents]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
What is happening?