1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| 1. test.c
void one_ptr_func(int* num, int length) { for(int i=0; i<length; i++) { num[i] *= 2; } }
void two_ptr_func(int** num, int row, int column) { for(int i=0; i<row; i++) { for(int j=0; j<column; j++) { num[i][j] *= 2; } } }
void three_ptr_func(int*** num, int x, int row, int column) { for(int a=0; a<x; a++){ for(int i=0; i<row; i++){ for(int j=0; j<column; j++){ num[a][i][j] *= 2; } } } }
2. test.py from ctypes import *
lib = CDLL('./test.so')
data = [1,2,3,4,5] one_arr = (c_int*5)(*data) one_ptr = cast(one_arr, POINTER(c_int))
lib.one_ptr_func(one_ptr, 5) for i in range(5): print(one_ptr[i], end=' ') print("\n")
data = [(1,2,3), (4,5,6)] two_arr = (c_int*3*2)(*data) one_ptr_list = [] for i in range(2): one_ptr_list.append(cast(two_arr[i], POINTER(c_int))) two_ptr=(POINTER(c_int)*2)(*one_ptr_list)
lib.two_ptr_func(two_ptr, 2, 3) for i in range(2): for j in range(3): print(two_ptr[i][j], end=' ') print("\n")
data =[((1,2,3), (4,5,6)),((7,8,9), (10,11,12))] three_arr =(c_int*3*2*2)(*data) two_ptr=[] for i in range(2): one_ptr=[] for j in range(2): one_ptr.append(cast(three_arr[i][j], POINTER(c_int))) two_ptr.append((POINTER(c_int)*2)(*one_ptr)) three_ptr = (POINTER(POINTER(c_int))*2)(*two_ptr)
lib.three_ptr_func(three_ptr, 2, 2, 3) for i in range(2): for j in range(2): for k in range(3): print(three_ptr[i][j][k], end=' ') print("\n")
|