取消引用在Python中使用ffi.addressof创建的指针CFFI(C*-运算符等价物?)

取消引用在Python中使用ffi.addressof创建的指针CFFI(C*-运算符等价物?),python,c,pointers,dereference,python-cffi,Python,C,Pointers,Dereference,Python Cffi,使用Python CFFI,上面的代码将创建一个指向values作为pValue的第一个元素的指针 然后,您可以使用值[0]访问其内容,但这并不是真正透明的,而且有时不方便跟踪什么标记是什么值 是否有诸如C*-运算符、函数或其他东西之类的东西来取消对pValue的引用并直接访问其内容 在其他语言中…: values = ffi.new( "int[]", 10 ) pValue = ffi.addressof( pInt, 0 ) 编辑: 下面是一个有用的用例: 我觉得这样做更具可读性: //

使用Python CFFI,上面的代码将创建一个指向
values
作为
pValue
的第一个元素的指针

然后,您可以使用
值[0]
访问其内容,但这并不是真正透明的,而且有时不方便跟踪什么标记是什么值

是否有诸如C
*-运算符
、函数或其他东西之类的东西来取消对
pValue的引用并直接访问其内容

在其他语言中…:

values = ffi.new( "int[]", 10 )
pValue = ffi.addressof( pInt, 0 )
编辑:
下面是一个有用的用例:

我觉得这样做更具可读性:

// In C:
// =====

int values[ 10 ] = {0};
int* pValue = &( values[ 0 ] );

func_with_pointer_to_int_as_param( pValue );

printf( "%d\n", *pValue );

-------------------------------------------------------------

# In Python with CFFI:
# ====================

values = ffi.new( "int[]", 10 )
pValue = ffi.addressof( values, 0 )

lib.func_with_pointer_to_int_as_param( pValue ) #lib is where the C functions are

print values[ 0 ] #Something else than that? Sort of "ffi.contentof( pValue )"?
而不是:

pC_int = ffi.new( "int[]", 2 )
pType  = ffi.addressof( pC_int, 0 )
pValue = ffi.addressof( pC_int, 1 )
...

# That you access with:
print "Type: {0}, value: {1}".format( pC_int[ 0 ], pC_int[ 1 ] )

我猜前者更快。但是,当您想要访问这些值时,就不方便记住,例如“ok type是数字0”等等。

在C中,语法
*pType
总是等同于
pType[0]
。所以,假设你想做如下事情:

pInt_type = ffi.new( "int[]", 1 )
pType     = ffi.addressof( pInt_type, 0 )

pInt_value = ffi.new( "int[]", 1 )
pValue     = ffi.addressof( pInt_value, 0 )

...

# That you access with:
print "Type: {0}, value: {1}".format( pInt_type[ 0 ], pInt_value[ 0 ] )
但这当然不是有效的Python语法。解决方案是,您始终可以这样重写它,这将成为有效的Python语法:

print "Type: {0}, value: {1}".format( *pType, *pValue )

在C语言中,
*x
完全等同于
x[0]
。是的。我添加了一个用例,在这个用例中,直接取消对CData指针的引用会很有用。对不起,我还是不明白。你想做什么?这就是用C写的
*pType
*pValue
?然后您可以编写它
pType[0]
pValue[0]
。当然,您也可以定义和使用自己的函数
def contentof(p):return p[0]
。问题解决了,我的错,我不明白您第一次评论的意思。你能不能写一个类似“
pValue
作为
ffi.addressof(pC_int,0)
的别名,你可以用
pValue[0]
访问它的内容作为答案,这样问题就可以解决了?谢谢
print "Type: {0}, value: {1}".format( pType[0], pValue[0] )