1

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?
4
  • 2
    It seems ambitious that for your first program in C you write one to be called from Python, and for your first program in Python you write one to call a C program. Why not get some familiarity with both languages before trying to integrate them? Commented Jan 26, 2023 at 19:33
  • @JohnColeman it's just test task for middle position, my native language is go :) Commented Jan 26, 2023 at 19:43
  • 1
    resArr is a local variable. It is undefined behavior to return it. Commented Jan 26, 2023 at 20:56
  • 1
    The linked "tutorial" is very basic. Read the actual ctypes documentation. Commented Jan 26, 2023 at 21:35

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, it works, now I can pass two arrays to the C function. But also I receive from C function pointer to the integer (first element from array), how can I get and print all array element?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.