脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2025/7/4 浏览:3 次 )

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

以上这篇python3利用ctypes传入一个字符串类型的列表方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:python单线程文件传输的实例(C/S)
下一篇:Python 实现文件打包、上传与校验的方法
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap