最近在写python扩展C的相关代码,遇到一个问题,专门上来求助!
是有关于python扩展C传参的问题,如果传的参数是整形和字符串,很容易处理,但是遇到二级指针怎么办?
如下例子中:该如何写关于 alloc_mem 函数的扩展模块呢?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* alloc_mem(char **p)
{
*p = (char *)malloc(100);
if (*p == NULL)
return NULL;
strcpy(*p, "hello python!");
return *p;
}
int main(int argc, char *argv[])
{
char *p;
printf("%s\n", alloc_mem(&p));
return;
}
I may have hit a dead end at the time, but later I solved the problem through other methods. I used the method of extending the C module in python, which is the PyObject mentioned in the comments.
That is, int or char * parameters are still used, but further operations are completed within the C function.
Still thank you flyzero, garfileo, and araraloren for their guidance.