title: Python calling C Shared Library with ctypes custom structures
body: <p>I'm calling a C shared library from Python on a Linux system.</p>
  
  <p>The problem I'm running into is the function inside the C library takes a pointer to a structure as an argument. It then mallocs the memory for an array of structures, populates the array with data and returns. So I've defined the function as</p>
  
  <pre><code>from ctypes import *
  class myStruct(Structure):
      _fields_ = [("id", c_uint), "name", c_char*256)]
  
  library.func.argtypes =  [POINTER(myStruct)]
  </code></pre>
  
  <p>Then I call it like so:</p>
  
  <pre><code>Myfoo = myStruct
  Foo = pointer(Myfoo)
  Bar = library.func(Foo)
  for i in range(Bar):
      print("id = %u, name = %s" % (Foo[i].id, Foo[i].name))
  </code></pre>
  
  <p>Bar contains the number of structures that were allocated by func.</p>
  
  <p>No matter what I do, I can't get any data out of Foo. I've tried multiple different variations on this for months. I can look at the logs from the C library and I know it's getting the data and returning it, but I can't seem to find a way to extract it from Python.</p>
  
  <p>Any thoughts?</p>