I have the following code:
lib.h
struct Node {
int index;
char name[10];
};
void getAllNodes(Node array[]);
libtest.py
from ctypes import *
class Node(Structure):
_fields_ = [("index", c_uint), ("name", c_char*10)]
lib = CDLL("/path/to/lib.so")
nodeCount = 5
NodeArray = Node * nodeCount
getAllNodes = lib.getAllNodes
pointers = NodeArray()
getAllNodes(pointers)
casted = cast(pointers, POINTER(NodeArray))
for x in range(0, nodeCount):
node = cast(casted[x], POINTER(Node))
print "Node idx: %s name: %s" % (node.contents.index, node.contents.name)
I must be close because the Node struct at index 0 has the correct values but the remainder are gibberish. What am I building incorrectly that's giving me the problem?