Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created October 27, 2010 04:48
Show Gist options
  • Save dcolish/648466 to your computer and use it in GitHub Desktop.
Save dcolish/648466 to your computer and use it in GitHub Desktop.
from ctypes import *
class Node(Structure):
pass
Node._fields_ = [("next", POINTER(Node)),
("foo", c_int)]
class List(Structure):
_fields_ = [("head", POINTER(Node))]
def __init__(self, foo):
self.head = pointer(Node(None, c_int(foo)))
def append(self, extra_foo):
p = self.head
while p.contents.next:
p = p.contents.next
p.contents.next = pointer(extra_foo)
def __str__(self):
value = ''
p = self.head
while p:
value += str(p.contents.foo)
p = p.contents.next
return value
def main():
l = List(0)
for x in range(1, 10):
l.append(Node(None, c_int(x)))
print l
if __name__ == '__main__':
main()
@dcolish
Copy link
Author

dcolish commented Oct 27, 2010

A little example of using ctypes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment