Question Details

No question body available.

Tags

python c ctypes cpython

Answers (4)

Accepted Answer Available
Accepted Answer
February 17, 2026 Score: 5 Rep: 37,156 Quality: High Completeness: 70%

You can use frombuffer() to get a ctypes object that you can cast into a cvoidp:

import ctypes

buf = bytearray(16) cbuf = (ctypes.cchar * len(buf)).frombuffer(buf) ptr = ctypes.cast(cbuf, ctypes.cvoid_p)

Live demo

February 17, 2026 Score: 3 Rep: 180,954 Quality: Medium Completeness: 60%

Not a unique answer, but I wanted to point out that using ctypes.cast on the bytearray marks it as non-resizable, so the pointer generated is safe to use and any attempt to re-size the bytearray will be prevented.

Example:

import ctypes as ct

buf = bytearray(b'abcdefghijklm') buf += b'nop' # re-size allowed at this point

ptr = ct.cast((ct.cchar * len(buf)).frombuffer(buf), ct.cvoidp)

print(ct.cast(ptr, ct.POINTER(ct.cchar))[:len(buf)]) # cast to a char* to read the data buf[5] = ord('z') # modify in place. print(ct.cast(ptr, ct.POINTER(ct.cchar))[:len(buf)]) # change is shown. try: buf += b'qrs'100 # make a large change that should force a buffer reallocation to the bytearray. except BufferError as e: print(e) # re-size will be prevented else: print(ct.cast(ptr, ct.POINTER(ct.c_char))[:len(buf)])

print()

buf = bytearray(b'abcdefghijklm') buf += b'nop' # re-size allowed

No cast, buf not marked as exported

ptr = ct.c_void_p(ct.addressof((ct.c_char len(buf)).frombuffer(buf)))

print(ct.cast(ptr, ct.POINTER(ct.cchar))[:len(buf)]) # cast to a char to read the data buf[5] = ord('z') # modify in place. print(ct.cast(ptr, ct.POINTER(ct.c_char))[:len(buf)]) # change is shown. try: buf += b'qrs'100 except BufferError as e: print(e) else: print(ct.cast(ptr, ct.POINTER(ct.cchar))[:len(buf)]) # incorrect data printed

Output:

b'abcdefghijklmnop'
b'abcdezghijklmnop'
Existing exports of data: object cannot be re-sized

b'abcdefghijklmnop' b'abcdezghijklmnop' b"\xd0\xbeS\xf1#\x02\x00\x00 'N\xbb\xf9\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00)\x9b2\x8f\x08\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00 'N\xbb\xf9\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00)\x9b2\x8f\x08\x00\x00\xf0\xbeS\xf1#\x02\x00\x00\xa8h]\xbb\xf9\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00`)\x9b2\x8f\x08\x00\x00\x10\xbfS\xf1#\x02\x00\x00 'N\xbb\xf9\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\xc0\x12\x18-\x8f\x08\x00\x000\xbfS\xf1#\x02\x00\x00\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x0e\x00\x00\x00\x00\xc0\x12\x18-\x8f\x08\x00\x00P\xbfS\xf1#\x02\x00\x00 'N\xbb\xf9\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x10\xbfS1\x8f\x08\x00\x00p\xbfS\xf1#\x02\x00\x00\x10\xbfS\xf1#\x02\x00\x00\x10\x00\x00\x00\x00\x00\x00\x000\xbfS1\x8f\x08\x00\x00\x90\xbfS\xf1#\x02\x00\x00 'N\xbb\xf9\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00P\xbfS1\x8f\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

When using ctypes.addressof, maintaining a reference to the object being referenced would fix the problem, e.g.:

cbuf = (ct.cchar * len(buf)).frombuffer(buf)
ptr = ct.cvoidp(ct.addressof(cbuf))

But if cbuf goes out-of-scope, ptr could be invalidated. ctypes.cast appears to mark the bytearray as non-resizable. Even if I deleted the cast references, bytearray could no longer be re-sized.

February 17, 2026 Score: 1 Rep: 581 Quality: Low Completeness: 40%
import ctypes 
data = bytearray(32)  
chararray = (ctypes.cchar * len(data)).frombuffer(data)  
ptr = ctypes.cast(chararray, ctypes.cvoidp)
print(ptr)

using from_buffer can help you deal with mutable objects

February 17, 2026 Score: 0 Rep: 9 Quality: Low Completeness: 50%

ctypes.cast() does not work directly on bytearray because bytearray is not a ctypes object. cast() expects either a ctypes instance or something that already exposes a compatible C representation.

Instead of casting the bytearray itself, you should obtain a pointer to its underlying buffer using ctypes utilities.

import ctypes

buf = bytearray(16) ptr = ctypes.cvoidp(ctypes.addressof( (ctypes.cchar * len(buf)).frombuffer(buf) ))

print(ptr)