title: Python ctypes and pointers as function arguments
body: <p>I wrote a C library to interface with a hardware device over USB, and have used the library successfully with other C code, so I know it works. I want to call this library from Python, and in order to do so I need to pass a struct by reference so that the library can modify it. The structure is defined:</p>
  
  <pre><code>typedef struct eventNode eventNode;
  struct eventNode{
      int channels;
      int samples;
      uint16_t** event;
      eventNode* next;
      eventNode* prev;
  }
  </code></pre>
  
  <p>and in Python I have defined:</p>
  
  <pre><code>class eventNode(Structure):
      pass
  
  eventNode._fields_ = [("channels", c_int),
              ("samples", c_int),
              ("event",POINTER(POINTER(c_uint16))),
              ("prev",POINTER(eventNode)),
              ("next",POINTER(eventNode))]
  </code></pre>
  
  <p>which I believe is correct. Essentially I used this to implement a linked list, which my library can modify by calling a method with signature <code>void getFromCAEN(int, char*, eventNode**, eventNode**)</code>. The last two arguments are pointers to pointers to the list head and tail, respectively. My python code creates these pointers by calling:</p>
  
  <pre><code>queueHead = POINTER(eventNode)()
  queueTail = POINTER(eventNODE)()
  </code></pre>
  
  <p>which should initially be NULL (None in ctypes) so that the library knows the list is empty. When I call this function with the line</p>
  
  <pre><code>caenlib.getFromCAEN(handle,buff,byref(queueHead),byref(queueTail))
  </code></pre>
  
  <p>a linked list should be formed by the library, which I then attempt to index through with the following block:</p>
  
  <pre><code>cur = queueHead
  while cur:
      i+=1
      print("event!",i)
      print(addressof(cur))
      print(cur.contents.channels,cur.contents.samples)
      print(cur.contents.event)
      print(cur.contents.next) #print next object in line
      cur = cur.contents.next
      print(cur)               #this prints a different address for some reason
  </code></pre>
  
  <p>And the output of all those print statements is this:</p>
  
  <pre><code>event! 1
  140486871191712
  4 150
  &lt;__main__.LP_LP_c_ushort object at 0x7fc5a60d9200&gt;
  &lt;__main__.LP_eventNode object at 0x7fc5a60d9170&gt;
  &lt;__main__.LP_eventNode object at 0x7fc5a60d9290&gt;
  </code></pre>
  
  <p>But the problem is, that even though I know there should be many events in the list, it only iterates once and then stops. The problem seems to be with those last few lines, that even though <code>cur</code> gets assigned to <code>cur.contents.next</code>, the address seems to change 0x...170 becomes 0x...290 after the assignment. Attempting to dereference <code>cur</code> after this point raises a NULL poitner exception. I think I'm missing something about pointers and assignment in Python, but I'm not sure what to do.</p>
  
  <p>PS the access to <code>cur.contents.channels</code> and <code>cur.contents.samples</code> returns exactly what I expect it to for the one object that I am able to access, indicating that it is working at least partially.</p>