- 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
这是我在python中的代码.现在我可以从c编译器调用函数Hello(x,y),也可以在C编译器窗口中看到这段代码的输出...有没有可能...如果可能的话,请给出主意...
# 回答1
不可能.Ctype用于从Python中调用带有C类型参数的C库函数.
# 回答2
好的……有没有可能用c++包装器代码来编写结构?
例如,这是阶乘ok的c语言包装代码.
- #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);
- }
在这里,我可以使用Fact()函数调用......代替函数,有没有可能在这段代码中编写结构
# 回答3
看起来你走上了正确的道路.
你的C语言让我摸不着头脑,但我相信你想做的事情是可能的.
# 回答4
无论如何,谢谢你的回复……我不知道这是否可能……好的,谢谢爸爸