首页 > 解决方案 > 如何将 python 列表发送到 C 字符串数组和 C 长整数数组?

问题描述

我试图将 python 列表发送到字符串/整数的 C 数组。我试图打印字符串数组,但它什么也没打印。

我实际上正在编写代码以使用 Python 从 .csv 文件中提取数据并尝试将此数据发送到 C 代码。当我尝试运行代码时,我收到了一些警告。

我从main(){}like调用这个函数string_arr_conversion(NULL,pFunc),但我看不到任何打印,我收到下面解释的警告。

我已经调试过,发现if(!PyArg_ParseTuple(args, "o", &str_arr)) {retun NULL;}当我调用上面的函数时,如果块返回null。

#include <Python.h>

static PyObject *string_arr_conversion(PyObject *self,PyObject *args){
    PyObject *str_arr;
    int i;
    if(!PyArg_ParseTuple(args, "o", &str_arr)){
        retun NULL;
    }
    int n=PyObject_Length(str_arr);
        if(n<0) {
            return NULL;
        }
    char arr[n][40];
    for(i=0; i<n;i++){
        PyObject *str=PyList_GetItem(str_arr,i);
        arr[i][40]=PyLong_FromString(str,NULL,10);
    }
    for(i = 0; i<n; i++){
        printf("\n Element is %c", arr[i]);
    }
    return arr;
}

我收到以下警告:

1.In string_err_conversion function passing argument 1 of 'PyLong_FromString' from incompatible pointer type.  
note: expected 'char *' but argument is of type 'struct PyObject *'  
assignment makes integer from pointer without cast.{here            arr[i][MAX_STRING_SIZE]=PyLong_FromString(str,NULL,10); warnings are coming}  
2.return from incompatible pointer type and function returns address of local variable.{In this line ::return arr;}

标签: pythoncpython-embedding

解决方案


推荐阅读