Re: [ctypes-users] passing multidimensional array to c function
Brought to you by:
theller
From: Diez B. R. <de...@we...> - 2008-06-09 12:34:36
|
On Monday 09 June 2008 14:11:42 Mark Irvine wrote: > Hello, > > I'm trying to send a multidimensional array from python to a c function > using ctypes. > I found this post here, which helped me get this far: > http://sourceforge.net/mailarchive/message.php?msg_id=ll8n9yix.fsf%40python >.net > > > I have this c code: > > int getFromMultiArray(int (*list)[3]){ > int result; > int i,j,val; > > for(i=0;i<3;i++){ > for(j=0;j<3;j++){ > val = list[i][j]; > printf("i %d, j %d, val %d\n",i,j,val); > } > > } > result = list[1][1]; > return result; > } > > > I run it with this c code and it returns results as expected: > int main(void){ > > int list2[3][3] = { > {1,2,3}, > {4,5,6}, > {7,8,9}}; > > > int result = getFromMultiArray(list2); > printf("result %d \n",result); > > return 0; > } > > When I run it, I get: > i 0, j 0, val 1 > i 0, j 1, val 2 > i 0, j 2, val 3 > i 1, j 0, val 4 > i 1, j 1, val 5 > i 1, j 2, val 6 > i 2, j 0, val 7 > i 2, j 1, val 8 > i 2, j 2, val 9 > result 5 > > In this case, it returns element [1][1] which is 5, so I think this is > working just as i expect it to. > > > Now I'm trying to do the same from python with ctypes. > here's what I've done: > > > from ctypes import * > cdll.LoadLibrary('./_pointers.so') > > p = CDLL('./_pointers.so') > > threeInts_type = ARRAY( POINTER(c_int), 3) > l1 = c_int * 3 > l1 = l1(1,2,3) > l2 = c_int * 3 > l2 = l2(1,2,3) > l3 = c_int * 3 > l3 = l3(1,2,3) > > print "l1[0]: ",l1[0] > > threeIntArray = threeInts_type(l1,l2,l3) Shouldn't this be threeIntArray = threeInts_type(pointer(l1), pointer(l2), pointer(l3)) Diez |