我有以下代码:
#include <include/python3.5m/Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h"
#include<iostream>
int main (int argc, char *argv[]) {
PyObject *pName, *pModule, *pFunc, *pArray, *pReturn;
Py_Initialize();
import_array();
npy_intp dims[2]={4, 8};
double c_arr[4][8];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 8; j++)
c_arr[i][j] = i * 8 + j;
pName = PyUnicode_DecodeFSDefault("pyFile");
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, "pyFn");
pArray = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, reinterpret_cast<void*>(c_arr));
pReturn = PyObject_CallFunctionObjArgs(pFunc, pArray, NULL);
PyArrayObject *np_ret = reinterpret_cast<PyArrayObject*>(pReturn);
int *c_out = reinterpret_cast<int*>(PyArray_DATA(np_ret));
for (int i = 0; i < PyArray_SHAPE(np_ret)[0]; i++)
printf("%d ", c_out[i]);
printf("\n");
return 0;
}
列表[23,11,26,2]由 pyFn 在 pyFile.py 中作为数组返回:np.array([23,11,26] ,2])。
当我运行此代码时,输出为: 23 0 11 0
我有以下问题:
为什么在将C ++作为整数数组投射后打印数组时会出现0?我应该以其他方式投射c_out吗?
为什么我会收到警告&#34;转换为非指针类型&#39; int&#39;来自NULL [-Wconversion-null] import_array()&#34; 当我编译代码时?