python 向c 传递二维int类型的指针,char类型的指针

  1. int类型的
    c_int_arrs = []
    for num in range(len(p_num_temp)):
        lead_pos_arr = (ctypes.c_int*p_num_temp[num])()
        for i in range(p_num_temp[num]):
            lead_pos_arr[i] = p_pos_list[num][i]
        c_int_arrs.append(ctypes.cast(lead_pos_arr,ctypes.POINTER(ctypes.c_int)))
    P_Locs_temp=(ctypes.POINTER(ctypes.c_int)*2)(*c_int_arrs)
    这里的2,指的是c_int_arrs的个数
  2. char类型
    char**P的问题:
    
    c_char_datas = (ctypes.c_char_p * file_num)()
    for j in range(file_num):
        c_char = (ctypes.c_char*6)()
        c_char.value = file_list[j].name.encode('utf-8')
        c_char_datas[j] = ctypes.c_char_p(c_char.value)
    
    
  3. import array

    = array.array('d',[1,2,3])

    print(a)

    ptr = a.buffer_info()  # 返回tuple:(地址, 长度)

    print(ptr[0])

    print(ctypes.cast(ptr[0], ctypes.POINTER(ctypes.c_double)))  # 目标地址存入指针

  4.  # Cast from array.array objects

        def from_array(self, param):

            if param.typecode != 'd':

                raise TypeError('must be an array of doubles')

            ptr, _ = param.buffer_info()

            return ctypes.cast(ptr, ctypes.POINTER(ctypes.c_double))

     

        # Cast from lists/tuples

        def from_list(self, param):

            val = ((ctypes.c_double)*len(param))(*param)

            return val

     

        from_tuple = from_list

     

        # Cast from a numpy array

        def from_ndarray(self, param):

            return param.ctypes.data_as(ctypes.POINTER(ctypes.c_double))

你可能感兴趣的:(python)