I have the following C struct:
typedef struct {
uint8_t a;
uint8_t b;
uint32_t c;
uint8_t* d;
}
With ctypes, via a callback, I am able to obtain a pointer to such a struct in Python, let's call it ref
. I can easily obtain a, b, c this way:
from ctypes import cast, c_uint8, c_uint32, POINTER
a = cast(ref, POINTER(c_uint8)).contents.value
b = cast(ref + 1, POINTER(c_uint8)).contents.value
c = cast(ref + 2, POINTER(c_uint32)).contents.value
but I can't read the bytes from d. I tried the following:
d_pointer = cast(ref + 6, POINTER(POINTER(c_uint8))).contents
first_byte_of_d = d_pointer.contents
print type(first_byte_of_d) # prints <class 'ctypes.c_ubyte'>
print first_byte_of_d
At this last line I encounter a SIGSEGV when debugging with gdb. So the question is, how should one access the first byte of a pointer from a struct in Python?