from cffi import FFI ffi = FFI() lib = ffi.dlopen("mylib.so") text = ffi.new("char[]", b"hello") lib.print_string(text)
from cffi import FFI ffi = FFI() lib = ffi.dlopen("mylib.so") result = ffi.new("char[]", 100) input_string = "hello" lib.my_function(input_string, result) print(ffi.string(result))In this example, we allocate memory for an output string of 100 characters and a Python string input `hello`. We pass these to a C function `my_function()` which modifies the output string. We then print the resulting string. The package library for these examples is `mylib.so`, a custom C library that we need to load using Python cffi's `dlopen()` function.