CFFI idea? Dereferencing a pointer created with ffi.addressof (C *-operator equivalent?)

已查看 45 次
跳至第一个未读帖子

Damien Ruiz

未读,
2016年7月20日 12:43:402016/7/20
收件人 python-cffi
Hello,

Here is a copy and paste of a question I posted yesterday on stack overflow. A cute C-typo-friendly function could be added to CFFI if there is no support for that already (an equivalent to a C pointer dereference).

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

The code above creates a pointer to the first element of values as pValue.


You can then access its content with values[ 0 ], but this is not really transparent and it is sometimes inconvenient to keep track of what indice is what value.

Is there anything such as the C *-operator, a function or something else, to dereference pValue and access its content directly?


In other languages... :


// 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 )"?


Regards,


Damien Ruiz

Damien Ruiz

未读,
2016年7月28日 19:22:172016/7/28
收件人 python-cffi
Note that Armin answered my question on the stack overflow thread. :)

Thanks again!
回复全部
回复作者
转发
0 个新帖子