ctypes.cast.

>>> import ctypes
>>> c_long_p = ctypes.POINTER(ctypes.c_long)
>>> some_long = ctypes.c_long(42)
>>> ctypes.addressof(some_long)
4300833936
>>> ctypes.cast(4300833936, c_long_p)
<__main__.LP_c_long object at 0x1005983b0>
>>> ctypes.cast(4300833936, c_long_p).contents
c_long(42)
Answer from habnabit on Stack Overflow
🌐
Python
docs.python.org › 2.5 › lib › ctypes-pointers.html
14.14.1.14 Pointers
December 23, 2008 - Calling the pointer type without an argument creates a NULL pointer. NULL pointers have a False boolean value: >>> null_ptr = POINTER(c_int)() >>> print bool(null_ptr) False >>> ctypes checks for NULL when dereferencing pointers (but dereferencing non-NULL pointers would crash Python):
Discussions

Why is there no concept of a pointer or pass-by reference in python? Is there something I can use
Holy cow calm down. EXCEPT THERE IS NO TRUE MULTITHREADING Lol yes there is. The threading module makes real threads. If you are upset about the GIL you just need to learn how to use it. why is there no pointers!? All python names are pointers. It's incredibly hard not to use a pointer. No idea what you are doing but if you mutate a dictionary in a function you will see it in the calling scope. >>> def add_data(d): ... d['Fuck-Me-With-RAM'] = 'pure zen' ... >>> data = {} >>> add_data(data) >>> data {'Fuck-Me-With-RAM': 'pure zen'} Read this: https://nedbatchelder.com/text/names.html this junk language Cheers, don't let the door hit ya More on reddit.com
🌐 r/learnpython
16
0
August 10, 2022
[FEAT] being able to enforce the dereferencing of the smart pointer in argument conversion
To wrap it in Python safely, I'm trying to hold instances in a smart pointer (a la shared_ptr) that checks on dereferencing whether the referred to object still exists. More on github.com
🌐 github.com
21
December 29, 2020
python - Is it possible to dereference variable id's? - Stack Overflow
Can you dereference a variable id retrieved from the id function in Python? For example: dereference(id(a)) == a I want to know from an academic standpoint; I understand that there are more practi... More on stackoverflow.com
🌐 stackoverflow.com
c - Pointer to a pointer dereference - Stack Overflow
Since arr is a pointer to a pointer to an int, *arr+1 is a pointer to an int. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Ubuntu Forums
ubuntuforums.org › archive › index.php › t-895804.html
[SOLVED] Pointers in Python? [Archive] - Ubuntu Forums
In a C-ish way, let's say in Python everything is a pointer (passed by reference, is the technical term). :) To check if an object is the same as another (the same instance), use the 'is' operator, not '=='. ('is' is like comparing pointers and '==' like comparing the values of the dereferenced pointers).
🌐
tentacles666
tentacles666.wordpress.com › 2012 › 01 › 21 › python-ctypes-dereferencing-a-pointer-to-a-c
Python ctypes: dereferencing a pointer to a C structure | tentacles666
March 19, 2013 - First, you need to define a Python class that represents struct foo: class Foo (ctypes.Structure): _fields_ = [ ("bar", c_int) ] Next, you need to tell ctypes that newfoo() returns a pointer to such a thing: mylib.newfoo.restype = POINTER(Foo) Finally, after invoking newfoo(), you need to use the pointer’s contents member to dereference it: p = mylib.newfoo() p.contents.bar = 17 ·
🌐
Medium
medium.com › @mengdaliu › moving-from-a-programming-language-eg-cf114c403607
Pointers in Python v.s. Pointers in Golang | by Shar-Pei Dog | Medium
June 17, 2019 - Besides using the non-local statement in Python, the answer is to pass in a pointer variable, dereference it inside the function to get what’s actually held in the memory address, and then change the data directly.
🌐
Reddit
reddit.com › r/learnpython › why is there no concept of a pointer or pass-by reference in python? is there something i can use
r/learnpython on Reddit: Why is there no concept of a pointer or pass-by reference in python? Is there something I can use
August 10, 2022 -

I am a C++ and assembly developer mainly, but as someone entering the job market that just doesn't cut it. I have a job I got hired for where I'm taking 4+ years of a python codebase that no one has documented and is spaghetti and updating it, and was supposed to be adding multithreading (EXCEPT THERE IS NO TRUE MULTITHREADING SO I HAVE TO DO MULTIPROCESSING WHICH IS A CLUSTER-F***). So that blows.

BUT

why is there no pointers!? or way to go references? I need to store data in an object that gets passed into some libraries, and then the library runs callbacks where I analyze the crap. From there I need to set the values so in other callbacks I can work with the values. It's a dictionary so I can add some new keys to store the data... except I can't because it's like pass by value so the data instantly gets deleted once it returns from the callback. If I passed in a pointer I'd be set, except that's not possible...

That's just one issue in a mirad of issues I'm facing with this junk language. Not to even mention the horribleness of everything using like 2gb of memory because each process copies the entire memory space of the head process....

Please tell me there's some kinda object or pointer thing I can use.

EDIT : I got the help I needed here, thanks everyone! sorry to trash the language a bit above I was just frustrated lol

Top answer
1 of 4
13
Holy cow calm down. EXCEPT THERE IS NO TRUE MULTITHREADING Lol yes there is. The threading module makes real threads. If you are upset about the GIL you just need to learn how to use it. why is there no pointers!? All python names are pointers. It's incredibly hard not to use a pointer. No idea what you are doing but if you mutate a dictionary in a function you will see it in the calling scope. >>> def add_data(d): ... d['Fuck-Me-With-RAM'] = 'pure zen' ... >>> data = {} >>> add_data(data) >>> data {'Fuck-Me-With-RAM': 'pure zen'} Read this: https://nedbatchelder.com/text/names.html this junk language Cheers, don't let the door hit ya
2 of 4
4
EXCEPT THERE IS NO TRUE MULTITHREADING SO I HAVE TO DO MULTIPROCESSING WHICH IS A CLUSTER-F*** If that's an issue for you then someone used the wrong tool for the wrong job. That said: most of the time asyncio's single-threaded event loop will do just fine. why is there no pointers!? Why should there be? It's a high-level language with a garbage collector. or way to go references? For any collection type or class you're already getting effectively pass-by-reference, but if you mean why won't def foo(x): x = 2 x = 1 foo(x) # x is still 1 here change x to 2: because in python there's no reason it should. Spooky action at a distance is generally discouraged and, just like in C/C++ you should avoid when you reasonably can. It's a dictionary so I can add some new keys to store the data... except I can't because it's like pass by value so the data instantly gets deleted once it returns from the callback. Dictionaries are not passed by value. def foo(x): x["new_key"] = "it's here" y = {} foo(y) print(y) A bit more technically: everything is effectively passed by reference, the language simply (deliberately) has immutable values for integers, floats, strings, and most 'primitive' types like that so there exists no way to express the concept of "edit the value of the python object this variable currently points at" for those primitives. The concept absolutely does exist for objects in general - anything done via the name (as opposed to reassigning the name) acts on the underlying python object. That's why the dictionary key is persisted in y outside the function in my example.
🌐
Netlify
pointerspy.netlify.app › reference
Reference - pointers.py
Dereference the pointer. ... Move/copy a value into the memory at the pointers address.
🌐
PyPI
pypi.org › project › pointers.py › 1.0.5
pointers.py
Please check your connection, disable any ad blockers, or try using a different browser.
Find elsewhere
🌐
Google Groups
groups.google.com › g › cython-users › c › uul_h95lKqM
cannot dereference pointer
Okay, I'm trying to convert a python object and return the equivalent C type. I have one of each C type declared on the stack in the calling code and passed by pointer to the conversion function. I hope it's clear what I'm trying to do, even if it isn't elegant. I'm getting errors such as the following, one for each pointer I'm trying to dereference: Cannot assign type 'int' to 'int *' So the dereference isn't understood?
🌐
GitHub
github.com › pybind › pybind11 › issues › 2751
[FEAT] being able to enforce the dereferencing of the smart pointer in argument conversion · Issue #2751 · pybind/pybind11
December 29, 2020 - I tried to do a custom caster, but that ran into "specialization after instantiation" error messages. There is an "obvious path" to go through lambdas that all take the smart pointer and dereference themselves, but this is obviously a bit cumbersome when it needs to be done to a large number of methods.
Author: pybind
🌐
Python
bugs.python.org › issue24044
Issue 24044: NULL pointer dereference in listsort() with key function - Python tracker
April 23, 2015 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/68232
🌐
Lenovo
lenovo.com › home
Everything You Need to Know About the Dereference Operator | Lenovo UK
The dereference operator (*) is used with pointers to access the data pointed to by the pointer. On the other hand, the dot operator (.) is used with objects to access their member variables and member functions. Ensure that pointers are properly initialized and not null before dereferencing them.
🌐
Real Python
realpython.com › pointers-in-python
Pointers in Python: What's the Point? – Real Python
March 18, 2026 - Pointers are widely used in C and C++. Essentially, they are variables that hold the memory address of another variable. For a refresher on pointers, you might consider checking out this overview on C Pointers. In this article, you’ll gain a better understanding of Python’s object model and learn why pointers in Python don’t really exist.
Top answer
1 of 1
1
  1. Let's have a look at your Cython code
cdef double *c_arr3 = <double *>malloc(2*sizeof(double))
c_arr3[:] = [1.0, 2.0]

Here, the python list [1.0, 2.0] is only meant to initialise the block of memory on the heap your pointer c_arr3 points to. Therefore, there's no need to create a python list from a performance perspective. That's one of the reasons why Cython will roughly generate the following C code instead:

/* allocate the memory */
double* c_arr3 = (double*) malloc(2*sizeof(double));

/* create a C array (instead of a list) to initialize c_arr3 */
double tmp[2];
tmp[0] = 1.0;
tmp[1] = 2.0;

/* copy all values of tmp into c_arr3 */
memcpy(&c_arr3[0], &tmp[0], 2*sizeof(double));

The a[:] = expr is Cython's slicing syntax that is typically used for typed memoryviews and works pretty similar to numpy arrays. Thus, it's mainly used to copy values of the right-hand-side expr into a.

  1. c_arr3[:]and c_arr4[:] are no double*. It's just syntactic sugar in Cython for copying values (see the above C code), which also means that it can be used to initialize blocks of memory. However, at the time of writing, initialization via c_arr3[:] = expr only works for simple right-hand side literals like python lists.

  2. Yes, you could use a typed memoryview instead of raw pointers:

cdef double[::1] c_mv = <double[:2]>malloc(2*sizeof(double))

# set all values to 1.0
c_mv[:] = 1.0

# initialize it with the values of np_arr
# note that you don't need to convert np_arr to a list
c_mv[:] = np_arr[:]

# don't forget to free the memory!
free(&c_mv[0])
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-pointers-in-python-using-ctypes
Using Pointers in Python using ctypes - GeeksforGeeks
July 23, 2025 - In languages like C/C++ pointers ... that pointer variable i.e while printing it we put the certain format specifier and then use an asterisk (*) with that variable to get the value it was pointing....
🌐
Duke
jupyternotebooks-dpb6-e8e09e2d24a591fa64baaf90dc750c5415a29cf5e.pages.oit.duke.edu › 3-CPlusCPlus › 13-Pointers › 13-Pointers.html
13. Pointers — Programming for Financial Technology
To access the contents of the memory location that a pointer references, we use the unary operator * (dereferences). Going back to the third Python notebook, you can equate this address to the address of a building in a city - it just references a location in memory.