Home > database >  Pass array as parameter from python to C function
Pass array as parameter from python to C function

Time:01-29

the main idea of the question is How to pass array from python to the C function, get result (array to), and print this. I created C function with algorithm for calculation convolution. (It doesn't not work correctly, but it works, and it's my first program on C, so it's not pretty good, don't judge harshly):

#include <stdio.h>

int * convolution(int a[], int b[], int inputArraySize)
{
    int resArrayLength = inputArraySize*2-1;
    int resArr[resArrayLength];

    for (int i = 0; i < inputArraySize; i  )
    {
        int aPointer = 0;
        int bPointer = i;
        int res = 0;
        for (int j = 0; j <= i; j  ){
            bPointer = i - j;
            res = res   (b[bPointer] * a[aPointer]);
            aPointer = aPointer   1;
        }
        resArr[i] = res;
    }


    for(int i = 0; i<inputArraySize/2; i  ){
       int temp = a[i];
       a[i] = a[inputArraySize-i-1];
       a[inputArraySize-i-1] = temp;
    }
    for(int i = 0; i<inputArraySize/2; i  ){
       int temp = b[i];
       b[i] = b[inputArraySize-i-1];
       b[inputArraySize-i-1] = temp;
    }

    for (int i = 0; i < inputArraySize-1; i  )
    {
        int aPointer = 0;
        int bPointer = i;
        int res = 0;
        for (int j = 0; j <= i; j  ){
            bPointer = i - j;
            res = res   (b[bPointer] * a[aPointer]);
            aPointer = aPointer   1;
        }
        resArr[resArrayLength-1-i] = res;
    }

    return resArr;
}

I created .so file using this command cc -fPIC -shared -o convolution.so convolution.c. Also I created python script (yeh, it's my first script on python) according to the guide from this article.

from ctypes import *

so_file = "./convolution.so"

convolution = CDLL(so_file)

print(type(convolution))
a = [1, 3, 5, 6]
b = [3, 5, 6, 7]
print(convolution.convolution(a, b, 4))

Here I try to create 2 arrays and pass to the C function. But it doesn't work as I expect. Output of the program:

<class 'ctypes.CDLL'>
Traceback (most recent call last):
  File "t.py", line 10, in <module>
    print(convolution.convolution(a, b, 4))
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
  1. How can I pass this two arrays to the C function?

Also C function returns pointer to the first element of the result array to the Python script.

  1. How to print all array elements using returned pointer from c function in the python?

CodePudding user response:

Those are Python array, you have to convert them to C arrays before using them as parameters for a C function:

  a = [1, 3, 5, 6]
  b = [3, 5, 6, 7]
  a_arr = (ctypes.c_int * len(a))(*a)
  b_arr = (ctypes.c_int * len(b))(*b)
  

To get and print the resulting array, you can set the returning ctype of the function using .restype, call it, and then get a slice from the resulting array, to set the dimension ([0:4]):

convolution.convolution.restype = ctypes.POINTER(ctypes.c_int)
tmp = convolution.convolution(a, b, 4)
res = tmp[0:4]
print(res)
  • Related