0

I am have tried to communicate c with python using python object.. in the c side I store values to a pointer variable.. and when I return it I get a python object in the python side.. is it possible to retrieve "those pointer values from the python object" in python side??

test.c file

#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.

I have modified to this please check How can I retrive values in python side

test.c
uint64_t * getvar()
{
    int i=0;
    uint64_t * vars=(uint64_t *)malloc(sizeof(uint64_t));

        for(i=0;i<3;i++)
        {
            var[i]=i;
            printf("%llu\n",var[i]);
        }
    return vars;
}
test1.c
static PyObject * getlist(PyObject * self, PyObject * args)
{
    int i;
    uint64_t *  mylist;
    mylist=getvar();
    for(i=0;i<3;i++)
        printf("%llu\n",mylist[i]);
    PyObject * my_list = PyCapsule_New((void *)my_list, "uint64_t", NULL);
    return my_list;
}
test.py

import test1
x=test.getvals()
print x

while compiling c files I get such warnings

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]

but still the values get printed,, How can I initialize 'vars'?? and how to retrieve values assigned in test.c in test.py

please suggest..

ranjan
  • 1,739
  • 5
  • 18
  • 22

1 Answers1

2

Your code is horribly broken. Your getval function is storing data in random places in memory. That's liable to corrupt and will probably eventually crash the program.

*var=5;

For this to work, var needs to be pointing to something. But var doesn't point to anything, because you never set it.

I'm gonna guess that you know python and are just starting with C. I'll suggest that you learn C by itself for a while before trying to use python and c together. Your C code shows you to be very confused about how C works, and its gonna be really hard to work with the Python/C api until that is remedied.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • It would really be helpful if you please correct the above code so that I can send an array of values from the c side and retrieve at python side – ranjan Apr 01 '12 at 04:38
  • Or is it possible to return an integer array than a python object – ranjan Apr 01 '12 at 04:49
  • how to return 64 bit integer value – ranjan Apr 01 '12 at 05:31
  • thank you so much for your help .. but cannot avoide the warning format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘uint64_t’ [-Wformat] – ranjan Apr 01 '12 at 08:13
  • @ranjan, you should either return a python list: http://docs.python.org/c-api/list.html, or use ctypes http://docs.python.org/library/ctypes.html as for the llu: http://stackoverflow.com/questions/8132399/how-to-printf-uint64-t – Winston Ewert Apr 01 '12 at 14:10