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

2
  • 4
    Add your code please ;) And remember that English is not completely case-insensitive... Commented Mar 31, 2012 at 13:05
  • using malloc removed the warning ‘vars’ may be used uninitialized in this function [-Wuninitialized] Commented Apr 1, 2012 at 7:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
Or is it possible to return an integer array than a python object
how to return 64 bit integer value
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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.