Dereferencing a pointer created with ffi.addressof in Python CFFI (C *-operator equivalent?)
NickName:DRz Ask DateTime:2016-07-20T01:21:11

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

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

With Python CFFI, 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 )"?

EDIT :
Here is a use case where it is useful:

I find it more readable to do:

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 ] )

Rather than:

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 ] )

And I guess the former is faster. But, when you want to access the values it makes it inconvenient to remember like "ok type is number 0" etc...

Copyright Notice:Content Author:「DRz」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38464789/dereferencing-a-pointer-created-with-ffi-addressof-in-python-cffi-c-operator

More about “Dereferencing a pointer created with ffi.addressof in Python CFFI (C *-operator equivalent?)” related questions

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

values = ffi.new( "int[]", 10 ) pValue = ffi.addressof( pInt, 0 ) With Python CFFI, the code above creates a pointer to the first element of values as pValue. You can then access its content with

Show Detail

How to cast a pointer to a Python cffi struct to System.IntPtr (.NET)?

I need to pass a System.IntPtr to a .NET function (Python with pythonnet). This pointer should refer to a struct created in cffi. I found this: from CLR.System import IntPtr, Int32 i = Int32(32) ...

Show Detail

Common Lisp CFFI: pointer to the pointer

I am trying to write the CFFI wrapper for Sundials CVODE library. SWIG was choking on Sundials headers since they are quite interconnected and SWIG couldn't find the right headers, so I did it by h...

Show Detail

Pandas series to CFFI pointer

I am trying to interop between Python and Rust code using CFFI module. I want to pass a Pandas series data pointer through C interface. However I don't know how the underlying series data is laid o...

Show Detail

Compiler warning C4383 for native pointer dereferencing

I use operator* to deference a pointer inside my C++/CLI class and the compiler gives a warning about this operation: C4383: 'instance_dereference_operator' : the meaning of dereferencing a handle...

Show Detail

Regarding definition of dereferencing and member selection operators in smart pointer

In smart pointer implementation, dereferencing operator and member selection operators are always defined as below. T& operator* () const // dereferencing operator { return *(m_pRawPo...

Show Detail

Best way to pass pointer to C function from Python using CFFI

I want to create a Python wrapper to a C function in a thirdparty library that has a signature such as int f(double* x); where the function f modifies the input argument x (i.e., call by reference

Show Detail

Dereferencing a pointer to incomplete type

Let's say we have two structs in a source file: struct B { int x; }; struct A { beta y; }; In the equivalent header file we have these: typedef B* beta; typedef A* alpha; Also,

Show Detail

Dereferencing pointer to incomplete type error for struct member access in Python swig C wrapper

What is the error while compiling swig Python wrapper under GCC 4.8.2? wfdb_python_wrap.c:3967:11: error: dereferencing pointer to incomplete type if (arg1->fname) free((char*)arg1->fname); ...

Show Detail

Build and install of cffi fails

(venv) ~/P/PickleballMaps ❯❯❯ pip install cffi==1.11.0 ...

Show Detail