【发布时间】:2021-02-25 06:49:19
【问题描述】:
我需要相同函数的两个实例(不仅仅是别名)。绝对有效的一件事是
void writedata(union chip *tempchip, unsigned char *datapos, int datanum)
{
blahblah
}
void writestring(union chip *tempchip, unsigned char *datapos, int datanum)
{
writedata(tempchip, datapos, datanum);
}
这有点傻,因为第二个只是将参数传递给第一个。所以我尝试变得“聪明”并做一个指针
void writedata(union chip *tempchip, unsigned char *datapos, int datanum)
{
blahblah
}
void (* writestring)(union chip *, unsigned char *, int) = writedata;
which on using 返回分段错误。为什么第二种方法不起作用?
编辑:我通过ctypes从Python调用这两个函数:
writedata = parallel.writedata
writedata.argtypes = [devpointer, POINTER(c_ubyte), c_int]
writestring = parallel.writestring
writestring.argtypes = [devpointer, c_char_p, c_int]
因为我想同时提供 strings 和 byte arrays 作为第二个参数。
【问题讨论】:
-
看起来不错,你能举个完整的例子吗?
-
您是从单独的文件中调用它吗?它需要与定义保持一致,即作为函数指针,而不是函数。
-
我通过
ctypes从Python调用这两个函数。 -
@Pygmalion 这就是问题所在。
ctypes试图将其作为函数调用。它需要将它作为指向函数的指针来调用,即它需要首先加载指针的值,然后调用它。writestring并不是writedata的别名。这是一个指向它的指针。 C 语法对此非常灵活,因此它可以显示 为别名,但事实并非如此。writestring已关联存储,用于指针。
标签: python c function pointers ctypes