This is a pre-release version (2.5.0.dev0+git20251130.2de293a). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / ctypeslib / as_array

function

numpy.ctypeslib:as_array

source: build-install/usr/lib/python3.14/site-packages/numpy/ctypeslib/_ctypeslib.py :520

Signature

def   as_array ( obj shape = None )

Summary

Create a numpy array from a ctypes array or POINTER.

Extended Summary

The numpy array shares the memory with the ctypes object.

The shape parameter must be given if converting from a ctypes POINTER. The shape parameter is ignored if converting from a ctypes array

Examples

Converting a ctypes integer array:
import ctypes
ctypes_array = (ctypes.c_int * 5)(0, 1, 2, 3, 4)
np_array = np.ctypeslib.as_array(ctypes_array)
np_array
Converting a ctypes POINTER:
import ctypes
buffer = (ctypes.c_int * 5)(0, 1, 2, 3, 4)
pointer = ctypes.cast(buffer, ctypes.POINTER(ctypes.c_int))
np_array = np.ctypeslib.as_array(pointer, (5,))
np_array

Aliases

  • numpy.ctypeslib.as_array