网络编程
位置:首页>> 网络编程>> Python编程>> python3利用ctypes传入一个字符串类型的列表方法

python3利用ctypes传入一个字符串类型的列表方法

作者:abc15766228491  发布时间:2021-06-10 20:33:36 

标签:python3,ctypes,字符串

c语言里:c_p.c


#include <stdio.h>

void get_str_list(int n, char *b[2])
{
printf("in c start");
for(int i=0;i<n;i++)
{
 printf("%s", *(b+i));
 printf("\n");
}
printf("in c end");
}

编译为动态库的命令:


gcc -o hello1.so -shared -fPIC c_p.c

python里:p_c.py


from ctypes import *
ll = cdll.LoadLibrary
lib = ll("./12_23_ctype_list/hello1.so")
n = 3
str1 = c_char_p(bytes("nihao", 'utf-8'))
str2 = c_char_p(bytes("shijie", 'utf-8'))

a = (c_char_p*2)(str1, str2)
lib.get_str_list(2, a)

输出


in c startnihao
shijie
in c end

来源:https://blog.csdn.net/abc15766228491/article/details/78882605

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com