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
", 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

原文地址:https://www.cnblogs.com/douzujun/p/10822031.html