Python double pointer

I am trying to get values ​​from a pointer to a float array, but it returns as c_void_p in python

Code C

double v; const void *data; pa_stream_peek(s, &data, &length); v = ((const float*) data)[length / sizeof(float) -1]; 

Python bye

 import ctypes null_ptr = ctypes.c_void_p() pa_stream_peek(stream, null_ptr, ctypes.c_ulong(length)) 

The problem is when null_ptr is int (memory address?), But there is no way to read the array ?!

+4
source share
4 answers

My ctypes are rusty, but I believe you want to use POINTER (c_float) instead of c_void_p.

So try the following:

 null_ptr = POINTER(c_float)() pa_stream_peek(stream, null_ptr, ctypes.c_ulong(length)) null_ptr[0] null_ptr[5] # etc 
+3
source

To use ctypes in a way that mimics your C code, I would suggest (and I got out of practice, and this has not been verified):

 vdata = ctypes.c_void_p() length = ctypes.c_ulong(0) pa_stream_peek(stream, ctypes.byref(vdata), ctypes.byref(length)) fdata = ctypes.cast(vdata, POINTER(float)) 
+1
source

You also probably want to pass null_ptr using byref, for example.

 pa_stream_peek(stream, ctypes.byref(null_ptr), ctypes.c_ulong(length)) 
0
source

When you pass pointer arguments without using ctypes.pointer or ctypes.byref, their contents are simply set to the integer value of the memory address (i.e., the bits of the pointer). These arguments must be passed using byref (or pointer , but byref has less overhead):

 data = ctypes.pointer(ctypes.c_float()) nbytes = ctypes.c_sizeof() pa_stream_peek(s, byref(data), byref(nbytes)) nfloats = nbytes.value / ctypes.sizeof(c_float) v = data[nfloats - 1] 
0
source

All Articles