Call the python function from C compiler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashi59
    New Member
    • Mar 2007
    • 17

    Call the python function from C compiler

    Code:
    from ctypes import *
    class HELLO(Structure):
        _fields_=[("a",c_int),
                  ("b",c_int)]
    x=input()
    y=input()
    hai=HELLO(x, y)
    print hai.a,hai.b
    This my code in python.now i can call the function HELLO(x,y) from c compiler and also can i see the output of this code in C compiler window...Is it possible....If its possible plz give idea...
    Last edited by bartonc; Mar 22 '07, 09:07 AM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by shashi59
    Code:
    from ctypes import *
    class HELLO(Structure):
        _fields_=[("a",c_int),
                  ("b",c_int)]
    x=input()
    y=input()
    hai=HELLO(x, y)
    print hai.a,hai.b
    This my code in python.now i can call the function HELLO(x,y) from c compiler and also can i see the output of this code in C compiler window...Is it possible....If its possible plz give idea...
    Not possible. ctypes is used for calling C library functions with C type arguments FROM Python.

    Comment

    • shashi59
      New Member
      • Mar 2007
      • 17

      #3
      ok....Is there any possible to write structure in c wrapper code
      Example this is my c wrapper code for factorial ok....
      Code:
      #include <Python.h>
      
      int fact(int n)
      {
          if ((n == 0)||(n==1))
              return 1;
          else
              return fact(n*fact(n-1));
      }
      
      static PyObject* fact(PyObject* self, PyObject* args)
      {
          const char *command;
          int n;
      
          if (!PyArg_ParseTuple(args, "i", &n))
              return NULL;
      
          return Py_BuildValue("i", fact(n));
      }
      
      static PyMethodDef FactMethods[] = {
          {"fact", fact, METH_VARARGS, "Calculate the Factorial of given numbers."},
          {NULL, NULL, 0, NULL}
      };
      
      PyMODINIT_FUNC
      initfact(void)
      {
          (void) Py_InitModule("fact", FactMethods);
      }

      here i could use fact() function calling .......instead of fuction Is there any possible to write structure in this code
      Last edited by bartonc; Mar 22 '07, 10:16 AM. Reason: added [code][/code] tags

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by shashi59
        ok....Is there any possible to write structure in c wrapper code
        Example this is my c wrapper code for factorial ok....
        Code:
        #include <Python.h>
        
        int fact(int n)
        {
            if ((n == 0)||(n==1))
                return 1;
            else
                return fact(n*fact(n-1));
        }
        
        static PyObject* fact(PyObject* self, PyObject* args)
        {
            const char *command;
            int n;
        
            if (!PyArg_ParseTuple(args, "i", &n))
                return NULL;
        
            return Py_BuildValue("i", fact(n));
        }
        
        static PyMethodDef FactMethods[] = {
            {"fact", fact, METH_VARARGS, "Calculate the Factorial of given numbers."},
            {NULL, NULL, 0, NULL}
        };
        
        PyMODINIT_FUNC
        initfact(void)
        {
            (void) Py_InitModule("fact", FactMethods);
        }

        here i could use fact() function calling .......instead of fuction Is there any possible to write structure in this code
        Looks like you are on the right track.
        And you are over my head with all that C stuff, but I'm sure that what you want to do is possible.

        Comment

        • shashi59
          New Member
          • Mar 2007
          • 17

          #5
          Anyway thanks your reply........i dont know its possible or not........ok thanks da

          Comment

          Working...