I am passing numpy arrays to C code using ctypes. I would like to keep that information in a C structure. I have the following code in C:
my_file.h
typedef struct container
{
int nb_rows;
int nb_cols;
double ** data;
};
void save_d2_double(int nb_rows, int nb_cols, double *data[nb_cols]);
void print_container();
my_file.c
#include <stdio.h>
#include "my_file.h"
struct container c;
void save_d2_double(int nb_rows, int nb_cols, double * data[nb_cols] ){
c.nb_rows = nb_rows;
c.nb_cols = nb_cols;
c.data = data;
}
void print_container(){
printf("\n");
printf("%d\t%d", c.nb_rows, c.nb_cols);
for(int i = 0; i < c.nb_rows; i++){
for(int j = 0; j < c.nb_cols; j++){
printf("%f\t", c.data[i][j]); \\ Error here
}
printf("\n");
}
printf("\n");
}
And the following in python:
my_file.py
import numpy as np
import numpy.ctypeslib as npct
import ctypes
LIBC = ctypes.CDLL("my_file.so")
array_2d_double = npct.ndpointer(dtype=np.double, ndim=2)
LIBC.save_d2_double.restype = None
LIBC.save_d2_double.argtypes = [ctypes.c_int, ctypes.c_int, array_2d_double]
LIBC.print_container.restype = None
LIBC.print_container.argtypes = []
# The shape of this array vary in the end application.
x_2d_d = np.array([[1.,2.,3.,10.],[4.,5.,6.,11.],[7.,8.,9.,12.]], dtype=np.float64)
shape = x_2d_d.shape
LIBC.save_d2_double(shape[0], shape[1], x_2d_d)
LIBC.print_container()
When I want to access c.data I get:
Segmentation fault (core dumped)
I suspect that the problem is in the declaration of double ** data; however, since C doesn't know in advance the dimensions of the numpy array, I don't know how to declare this filed.
Thanks in advance !