title: Passing Numpy arrays to a C function for input and output
body: <p><strong>Oh my word I'm a fool.</strong>
  I was simply omitting the second and third arguments when calling the function.
  Like a fool.
  Because that's what I am.
  Original silly question follows:</p>
  
  <p>This seems like it must be a very common thing to do, but I can't find a relevant tutorial, and I'm too ignorant about <code>Numpy</code> and <code>ctypes</code> to figure it out myself.</p>
  
  <p>I have a C function in file <code>ctest.c</code>.</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  void cfun(const void * indatav, int rowcount, int colcount, void * outdatav) {
      //void cfun(const double * indata, int rowcount, int colcount, double * outdata) {
      const double * indata = (double *) indatav;
      double * outdata = (double *) outdatav;
      int i;
      puts("Here we go!");
      for (i = 0; i &lt; rowcount * colcount; ++i) {
          outdata[i] = indata[i] * 2;
      }
      puts("Done!");
  }
  </code></pre>
  
  <p>(As you may guess, I originally had the arguments as double * rather than void *, but couldn't figure out what to do on the Python side.  I'd certainly love to change them back, but I'm not picky as long as it works.)</p>
  
  <p>I make a shared library out of it.
      gcc -fPIC -shared -o ctest.so ctest.c</p>
  
  <p>Then in Python, I have a couple numpy arrays, and I'd like to pass them to the C function, one as input and one as output.</p>
  
  <pre><code>indata = numpy.ones((5,6), dtype=numpy.double)
  outdata = numpy.zeros((5,6), dtype=numpy.double)
  lib = ctypes.cdll.LoadLibrary('./ctest.so')
  fun = lib.cfun
  # Here comes the fool part.
  fun(ctypes.c_void_p(indata.ctypes.data), ctypes.c_void_p(outdata.ctypes.data))
  
  print 'indata: %s' % indata
  print 'outdata: %s' % outdata
  </code></pre>
  
  <p>This doesn't report any errors, but prints out</p>
  
  <pre><code>&gt;&gt;&gt; Here we go!
  Done!
  indata: [[ 1.  1.  1.  1.  1.  1.]
   [ 1.  1.  1.  1.  1.  1.]
   [ 1.  1.  1.  1.  1.  1.]
   [ 1.  1.  1.  1.  1.  1.]
   [ 1.  1.  1.  1.  1.  1.]]
  outdata: [[ 0.  0.  0.  0.  0.  0.]
   [ 0.  0.  0.  0.  0.  0.]
   [ 0.  0.  0.  0.  0.  0.]
   [ 0.  0.  0.  0.  0.  0.]
   [ 0.  0.  0.  0.  0.  0.]]
  </code></pre>
  
  <p>The outdata array is not modified.  And in fact if I call the function again I get a segfault.  Which doesn't surprise me -- I really don't know what I'm doing here.  Can anyone point me in the right direction?</p>