Python: ctypes and Pointer to Structure
Question: I am trying to make a pointer of a struct and then de-reference it. But it's
crashing. I have mimic'ed the behvior here with this simple code.

    
    
    from ctypes import *
    import ctypes
    
    class File(Structure):
     _fields_ = [("fileSize", c_uint),
                ("fileName", c_byte * 32)]
    
    f = File()
    f.fileSize = 2
    print(f.fileSize)
    P = ctypes.POINTER(File)
    p = P.from_address(addressof(f))
    print(p.contents.fileSize)
    

Can someone point out what's the issue with this code?

Thanks in advance.


Answer: this works (I just tried):

    
    
    p = pointer(f)
    

no need to instantiate P at all. To be clearer, given the p and P look quite
similar on screen:

    
    
    from ctypes import *
    
    class File(Structure):
     _fields_ = [("fileSize", c_uint),
                ("fileName", c_byte * 32)]
    
    f = File()
    f.fileSize = 2
    print(f.fileSize)
    p = pointer(f)
    print(p.contents.fileSize)
    

