Python利用ctypes实现按引用传参

C的代码

void test_cref(char *a, int *b, char *data)
{
    char *p = (char*)wtk_calloc(20, sizeof(char));
    strcpy(p, "cute");
    a[0] = p[2];

    *b = 223;

    data = a;
    
    wtk_debug("%s %d %s\n", a, *b, data);
}

打包成动态库之后

from ctypes import *

libc = cdll.LoadLibrary("./libwtkdlg.so")

a = create_string_buffer("t".encode("utf-8"))

b = c_int(23)

d = create_string_buffer("douzi".encode('utf-8'))

print(a.value)
print(b.value)
print(d.value)

libc.test_cref(byref(a), byref(b), byref(d))

a改变了内容,没有改变指向,b改变了内容

更多: https://www.cnblogs.com/gaowengang/p/7919219.html

posted @ 2019-05-06 20:56  douzujun  阅读(1607)  评论(0编辑  收藏  举报