I have a shared C library with a struct I would like to use in my python code
struct my_struct {
char name[64];
};
so in python I recreate it with
class MyStruct(ctypes.Structure):
_fields_ = [
("name", ctypes.c_char*64)
]
when I check the type of MyStruct.name i get 'str', whereas I expected 'c_char_Array_64'.
s=MyStruct()
print type(s.name) # <type 'str'>
So when I set the 'name' and try to use it, C sees it as blank.
s.name="Martin"
lib=ctypes.cdll.LoadLibrary('./mylib.so')
lib.my_func(s) # prints ''
where lib is the shared C library loaded with ctypes and my_func simply prints struct->name
void my_func(struct my_struct *s){
printf("Hello %s\n", s->name);
}
I would like to know why ctypes.Structure converts the char-array to a string and how to use it in the case specified above.
Thank you
Update & Solution
Tnanks to @CristiFati for the help on debugging this problem. I have marked his answer as correct as it is in fact the answer to the question posted. In my case the problem was that the Structs were NOT of equal lengths in the Python and C program. So to whoever stumbles upon this question in the future, be very meticulous in checking that your Structs are in fact defined equally.