相关疑难解决方法(0)

python ctypes中的多维char数组(字符串数组)

我正在尝试使用ctypes将一个字符数组数组传递给C函数.

void cfunction(char ** strings)
{
 strings[1] = "bad"; //works not what I need.
 strings[1][2] = 'd'; //this will segfault.
 return;
}

char *input[] = {"foo","bar"};
cfunction(input);
Run Code Online (Sandbox Code Playgroud)

因为我抛出的数组是静态定义的,所以我只是更改了函数声明和输入参数:

void cfunction(char strings[2][4])
{
 //strings[1] = "bad"; //not what I need.
 strings[1][2] = 'd'; //what I need and now it works.
 return;
}

char input[2][4] = {"foo","bar"};
cfunction(input);
Run Code Online (Sandbox Code Playgroud)

现在我遇到了如何在python中定义这个多维字符数组的问题.我以为它会这样:

import os
from ctypes import *
libhello = cdll.LoadLibrary(os.getcwd() + '/libhello.so')
input = (c_char_p * 2)()
input[0] = create_string_buffer("foo")
input[1] = create_string_buffer("bar")
libhello.cfunction(input) …
Run Code Online (Sandbox Code Playgroud)

python ctypes multidimensional-array

5
推荐指数
1
解决办法
6845
查看次数

标签 统计

ctypes ×1

multidimensional-array ×1

python ×1