title: How to pass numpy array inside of struct to ctypes function?
body: <p>I'm trying to wrap some third-party code from a library in my Python application. Essentially, the function I would like to call takes a struct as input that houses (among other things) pointers to double arrays. A simplified example would be:</p>
  
  <p><strong>myfunc.h:</strong></p>
  
  <pre><code>typedef struct mystruct_t {
      int n;
      double *x;
  } mystruct_t;
  
  double myfunc(mystruct_t* data);
  </code></pre>
  
  <p><strong>myfunc.c</strong>:</p>
  
  <pre><code>#include "myfunc.h"
  
  double myfunc(mystruct_t* data) {
      double result = 0.;
      for(int i = 0; i &lt; data-&gt;n; i++) {
          result += data-&gt;x[i];
      }
      return result;
  }
  </code></pre>
  
  <p><strong>Makefile</strong>:</p>
  
  <pre><code>CC = gcc
  CFLAGS = -g -Wall -fPIC -lm -std=gnu99
  
  all: libmyfunc.so
  
  m.PHONY : clean
  
  libmyfunc.so: myfunc.o
      gcc -shared -Wl,-soname,$@ -o $@ $^
  
  %.o: %.c
      $(CC) -c $(CFLAGS) $&lt;
  
  clean:
      rm -vf libmyfunc.so *.o
  </code></pre>
  
  <p>I would like to wrap <code>myfunc</code> using NumPy, <code>ctypes.Structure</code> and <code>numpy.ctypeslib</code> so that I can pass NumPy arrays to <code>myfunc</code> as attributes of <code>mystruct_t</code>. So far I've been trying the following:</p>
  
  <p><strong>myfunc.py:</strong></p>
  
  <pre><code>#!/usr/bin/env python
  import numpy as np
  import numpy.ctypeslib as npct
  import ctypes
  import os
  
  array_1d_double = npct.ndpointer(dtype=np.double, ndim=1, flags='C_CONTIGUOUS')
  
  
  class MyStruct(ctypes.Structure):
      _fields_ = [
          ('n', ctypes.c_int16),
          ('x', array_1d_double)
      ]
  
  libmyfunc = npct.load_library('libmyfunc', os.path.dirname(__file__))
  libmyfunc.myfunc.restype = ctypes.c_double
  libmyfunc.myfunc.argtypes = [
      ctypes.POINTER(MyStruct)
  ]
  
  x = np.array([1.0, 2.0, 3.0, 4.0])
  
  mystruct = MyStruct()
  mystruct.n = len(x)
  mystruct.x = x
  
  res = libmyfunc.myfunc(mystruct)
  </code></pre>
  
  <p>However, this fails with the following error message:</p>
  
  <pre><code>$ python myfunc.py
  Traceback (most recent call last):
    File "./myfunc.py", line 26, in &lt;module&gt;
      mystruct.x = x
  TypeError: cannot be converted to pointer
  </code></pre>
  
  <p>How do I define my function signatures correctly so that type conversion can happen? Or do I need to convert <code>x</code> somehow before assigning it to <code>mystruct.x</code>?</p>
  
  <p>Unfortunately I cannot change the signature of the method I would like to call and I would like to not have to write wrapper C code unless it is absolutely necessary. The other questions and resources that I've found on this only deal with either <code>ctypes.Structure</code>s or <code>numpy.ctypeslib</code>, but can you make them work together?</p>
  
  <p>I've uploaded my reduced example <a href="https://gist.github.com/sseemayer/11218162" rel="nofollow">as a gist</a> so you can use it as a starting point.</p>
  
  <p>Many thanks in advance!</p>