-
I have the following cdef: typedef struct AVFormatContext AVFormatContext;
extern AVFormatContext* avformat_alloc_context(void); I obtain an instance of format_context = lib.avformat_alloc_context();
print(ffi.typeof(format_context));
# <ctype 'struct AVFormatContext *'> When I try to get its address, the script exits with an error: ffi.addressof(format_context);
# TypeError: expected a cdata struct/union/array object Not sure what I'm doing wrong. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The type of
If you do want to get a pointer-to-pointer, you need to store the pointer you got into a C structure or array first, i.e. copy the pointer from the Python world to the C world. For example:
|
Beta Was this translation helpful? Give feedback.
The type of
format_context
is'struct AVFormatContext *'
, that is, it is already a pointer to the struct. Are you trying to get the address of that pointer, i.e. a pointer-to-pointer?ffi.addressof()
only works on things that have known C-level addresses. Theformat_context
Python variable is a Python object that internally points to that struct, but you can't take its own address because that pointer itself is not C-level data. Think about Python objects as living in a separate world, somewhere else than all the C data that cffi lets you manipulate. You can't get the address of a Python object without using some hacks that are specific to an implementation, say CPython, and these hacks w…