Skip to content
Advertisement

Pass a 2d numpy array to c using ctypes

What is the correct way to pass a numpy 2d – array to a c function using ctypes ? My current approach so far (leads to a segfault):

C code :

JavaScript

Python code:

JavaScript

Advertisement

Answer

This is probably a late answer, but I finally got it working. All credit goes to Sturla Molden at this link.

The key is, note that double** is an array of type np.uintp. Therefore, we have

JavaScript

And then use doublepp as the type, pass xpp in. See full code attached.

The C code:

JavaScript

The Python code:

JavaScript

Hope it helps,

Shawn

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement