python如何通过C函数更改值

时间:2019-07-10 07:22:49

标签: python c++ c python-3.x ctypes

我有这样的C函数

void a(uint32_t*** e) {
    cout << e << " " << *e << " " << **e << " " << *(**e+2) << " " << *e[0][0] << endl;
    *e = (uint32_t**) malloc(sizeof(uint32_t) * 4); 
    (*e)[0] = (uint32_t*) malloc(sizeof(uint32_t) * 2);
    *e[0][0] = 2;
    *(**e+1) = 3;
    *(**e+2) = 4;
    *(**e+3) = 5;
    *(**e+4) = 6;
    cout << e << " " << *e << " " << **e << " " << *(**e+2) << " " << *e[0][0] << endl;

    cout << (*e)[0][0] << endl;
    cout << (*e)[0][2] << endl;
    cout << (*e)[0][3] << endl;
    cout << (*e)[0][4] << endl;
    cout << (*e)[0][1] << endl;
}

当我用C称呼它时

uint32_t** elements;
a(&elements);

它将元素更改为一个新数组,但是当我通过python调用它时

from ctypes import *
def main():
    lib = cdll.LoadLibrary('/Work/cspace/libdemo.dylib')

    eles = c_uint32()
    p1 = pointer(eles)
    p2 = pointer(p1)
    e = pointer(p2)


    print(eles)
    print(p1)
    print(p2)
    print(e)
    lib.a(e)
    print(e.contents.contents.contents[0][2])
if __name__ == "__main__":
    main()
  1. 我发现python中e的地址与C中e的地址不同,为什么?

  2. 如何使用e从C获取数组?

0 个答案:

没有答案