After following this answer, I wrote a C library which I call from python using ctypes. The C library contains a function which takes a char* and modifies its contents. I would like to read the contents in Python, however, after calling the library function in Python, I get an empty string even though I see the correct output in the terminal if I include a printf statement in the C code. What am I doing wrong?
C code:
void somefunction(char * pythonbuffer)
{
/* do something */
printf("%s", pythonbuffer);
}
Python code:
lib.somefunction.argtypes = [ctypes.c_char_p]
lib.somefunction.restype = ctypes.c_void_p
buffer = ctypes.create_string_buffer(upper_limit)
lib.somefunction(buffer)
print(buffer.value)
Output:
b''
somefunctionmodify its parameterpythonbuffer? What did you expect to see happen?pythonbuffer. If I then print the content in C I get what I expect, but I can't get the same output in Python.lib.somefunction(ctypes.POINTER(buffer))POINTER(buffer)makes no sense, andpointer(buffer)would be redundant.