I tried to call c function in python here is my code
string.c
#include <stdio.h>
int print(const char *str)
{
printf("%s", str):
return 0;
}
string.py
from ctypes import *
so_print = "/home/ubuntu/string.so"
my_functions = CDLL(so_print)
print(my_functions.print("hello"))
when i run the python script it prints only the fist character of the string example "h"
How can i pass any string and my c code will read and display it.
Your function accepts a const char*, which corresponds to a Python bytes object (which coerces to c_char_p), not a str (which coerces to c_wchar_p). You didn't tell Python what the underlying C function's prototype was, so it just converted your str to a c_wchar_p, and UTF-16 or UTF-32 encoded string with solely ASCII characters looks like either an empty or single character (depending on platform endianness) C-style char * string.
Two things to improve:
Define the prototype for print so Python can warn you when you misuse it, adding:
my_functions.print.argtypes = [c_char_p]
before using the function.
Encode str arguments to bytes so they can be converted to valid C-style char* strings:
# For arbitrary string, just encode:
print(my_functions.print(mystr.encode()))
# For a literal, you can pass a bytes literal
print(my_functions.print(b"hello"))
# ^ b makes it a bytes, not str
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With