recursive ctypes structure accesing

There are no recursive structures in C, what you’ve got there as a member is a pointer to a structure. You can dereference the pointer simply by using

mylist = mylist.next1.contents

However, notice that your mylist != None is bogus. No ctype will ever equal to None like that.

Instead you need to restructure your loop as

while True:
    result += "fname:"+str(mylist.fname)
    result += "ftime:"+str(mylist.ftime)
    result += "fsize:"+str(mylist.fsize)
    if not mylist.next1:
        break

    mylist = mylist.next1.contents

Lastly some stylistic advice: your variable naming is off – mylist is not a list but a node/element in the list. And "fsize:"+str(mylist.fsize) seems very contrived to write, how about

results = []

    ...
    results.append(f"fname:{node.fname} ftime:{node.ftime} fsize:{node.size}")

result = " ".join(results)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top