I am having trouble combining C code with python via the ctypes library. I am trying to pass a python list into a C function which iterates over the list and modifies its values. I would now like to use the modified list in my python code.
C code - iterator.c
#include <math.h>
void apply_cosine(double *array,int length)
{
for(int i = 0 ; i < length; i++){
array[i] = cos(array[i]);
}
}
Python code - python_base.py
import os
import ctypes
current_dir = os.getcwd()
full_path = current_dir + "/lib.so"
_lib = ctypes.CDLL(full_path)
_lib.apply_cosine.argtypes = (ctypes.POINTER(ctypes.c_double), ctypes.c_int)
# _lib.apply_cosine.restype = ctypes.c_double
def apply_cosine(array: list) -> list:
'''Wrapper function to apply cosine function to each element in the list.
'''
length = len(array)
array_type = ctypes.c_double * length
array = _lib.apply_cosine(array_type(*array), ctypes.c_int(length))
return array
if __name__ == "__main__":
test = [12., 44., 23., 32., 244., 23.]
print(apply_cosine(test))
I am pretty sure that my problem is the return type. My c code only modifies the list via a pointer and does not return the list explicitly. Hence, the in the c code modified list seems not to get passed back into the python code.
Do you have any suggestions on how to do this properly?
Thank you very much in advance for your help.
_lib.apply_cosine(array_type(*array), ctypes.c_int(length)), without assigning anything toarray?return list(array)instead of the list comprehension