[問題] 如何把'list'變成'陣列'丟給C的函式?

看板Python作者 (WhiteRiverX)時間13年前 (2012/02/14 19:41), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/2 (看更多)
大家好: 小弟最近想把用Python整理過的list(裡面是float型態), 然後再把這個list丟給C的函式當引數。 兩個文件像下面這樣: _______ test.c ======= #include <stdio.h> double CalcUSRsimilar (double *x1, double *x2, int nAtom) { int i; for(i = 0; i < nAtom; i++) { printf("%f",*(x1+i) ); printf(" "); } double s; s=1; return s; } ===================================================================== _______ test.py ======= import ctypes USR_file = ctypes.cdll.LoadLibrary("./test.so") USR = USR_file.CalcUSRsimilar X=[1.2, 1.3] Y=[2.2, 2.4] print( USR( X, Y, 2 ) ) ===================================================================== 也就是在Python裡面宣告兩個list,然後再丟給C顯示出來。 test.c確定可以compile,問題卡在Python那關... ------------------------------------------------------------------------ Traceback (most recent call last): File "./test.py", line 12, in <module> print( USR( X, Y, 2 ) ) ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1 ------------------------------------------------------------------------ 小弟google一陣子了,但網路上的文章大多比較難(好像都是舉structure的例子... 但我應該不用用到那麼難吧!)。請各位大大務必提點小弟,感恩。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.183

02/14 19:59, , 1F
XD
02/14 19:59, 1F
文章代碼(AID): #1FEaXykt (Python)
文章代碼(AID): #1FEaXykt (Python)