Using ctypes in python I can freely pass an array to a C function using a pointer to its first element since an array decays to a pointer to its first element in C. So for the C function signature
void insert_sort(int A[], size_t length);
I can implement the following python wrapper.
import ctypes
from typing import List, Callable
def wrap_function(lib: ctypes.CDLL, name: str, restype: object, argtypes: List[object]) -> Callable:
"""Simplify wrapping ctypes functions"""
func = lib.__getattr__(name)
func.restype = restype
func.argtypes = argtypes
return func
lib = ctypes.CDLL("path/to/library.so")
insert_sort = wrap_function(lib, 'insert_sort', None, [ctypes.POINTER(ctypes.c_int), ctypes.c_size_t])
But C also allows one to have arrays of array and whats more variable sized arrays of arrays. For example the C function signature below demonstrates this.
void matrix_multiply(size_t n, int A[n][n], int B[n][n], int C[n][n]);
How would one wrap this function? To codify my question what goes in the blank below?
matrix_multiply = wrap_function(lib, 'matrix_multiply', None, [...What goes here...])
If the size of the arrays were known we could simply write
matrix_multiply = wrap_function(lib, 'matrix_multiply', None, [
ctypes.c_size_t, ((ctypes.c_int * 4)*4), ((ctypes.c_int * 4)*4), ((ctypes.c_int * 4)*4)
])
for arrays of size 4. But what can we do when this number is unknown?