SWIG+c+Python: Passing and receiving c arrays

℡╲_俬逩灬. 提交于 2019-12-06 15:54:01

A Python list is quite different from a C array. In C the name of an array is a pointer to a contiguous block of memory containing its elements. Python lists are complicated datastructures where memory isn't contiguous and simple address calculations do not hold. So you cannot expect C code meant for C arrays to work with Python lists.

You can access Python lists from C as described here:

http://effbot.org/zone/python-capi-sequences.htm

Alright, now I have it. As written in the EDIT above, with numpy.i the arrays can be wrapped quite comfortably. What I did not see was, that the ARGOUT Array does not want an array as Input, as in C. There is just the dimension needed. So, with the Code above, the Script

import bsp
import numpy as np
a = np.array([1, 1], dtype=np.int32)
b = np.array([1, 1], dtype=np.int32)

c = bsp.add(a, b, np.shape(a)[0])

print(c)

Gives the desired Output

[2 2]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!