In C
I have a function that expects array of unsigned char
s
void writedata(unsigned char *datapos, int datanum)
I would like to pass a standard string from Python instead
writedata = parallel.writedata
writedata.argtypes = [POINTER(c_ubyte), c_int]
a = "test string"
writedata(a, 11)
As far as I understand, string is actually an array of bytes/chars, and a
is a pointer. However, ctypes
disagrees:
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected LP_c_ubyte instance instead of str
How can I get "real" pointer from a string?
EDIT: David Cullen provided a solution that takes string pointer as a parameter:
writedata.argtypes = [c_char_p, c_int]
That's fine, but I would like to supply both byte arrays and strings to the function. This means that this should also work
ll = [0,1,2,3,4,5,6,7]
uints = (c_ubyte*8)(*ll)
writedata(uints, 8)
I am curious why I can't do both, because in terms of memory I think byte arrays and strings should be the same? Perhaps this is all about pointer conversions?
I also tried make two ctypes
connections to the same C
function, and this does not work.
SOLUTION: I have reformulated the question and received the best answer here: