我尝试过用python对象与c和python通信。。在c端,我把值存储到一个指针变量。。当我返回它时,我在python端得到了一个python对象。。是否可以在python端检索“那些来自python对象的指针值”??在
test.c文件
#include <stdio.h>
#include <stdlib.h>
int *getval() {
int *var;
int *var1;
int i;
var1=var;
*var=5;
var++;
*var=6;
return var1;
}
test1.c file
#include <Python.h>
static PyObject * getvals(PyObject * self, PyObject * args)
{
int *x;
x=getval();
PyObject * test = PyCapsule_New((int *)x, "int", NULL);
return test;
}
python file
import test1
x=test1.getvals()
something like this .. I want to get the values of the var file I have assigned in test.c in python file.
我已经修改了这个请检查如何在python端检索值
^{pr2}$在编译c文件时,我得到了这样的警告
format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘uint64_t’ [-Wformat]
warning: ‘vars’ may be used uninitialized in this function [-Wuninitialized]
但是值还是被打印出来了,如何初始化“vars”??以及如何在中检索test.c中指定的值测试.py在
请建议。。在
你的代码被严重破坏了。您的
getval
函数正在内存中随机存储数据。这很容易导致腐败,并可能最终导致程序崩溃。在要使其工作,
var
需要指向某个对象。但是var没有指向任何东西,因为你从来没有设置过它。在我想你可能了解python,而且只是从C开始,我建议你先自学C,然后再尝试将python和C结合使用。您的C代码表明您对C是如何工作的感到非常困惑,在这种情况得到纠正之前,使用Python/capi是非常困难的。在
相关问题 更多 >
编程相关推荐