I have the following lib.c
:
#include <stdio.h>
double var = 4;
double* fun() {
printf("%zd %f\n", &var, var);
return &var;
}
and the following main.py
:
import ctypes
lib = ctypes.cdll.LoadLibrary('lib.so')
fun = lib.fun
fun.restype = ctypes.POINTER(ctypes.c_double)
print ctypes.addressof(fun().contents)
How can I, inside the .py
, change the double
stored at the address of fun().contents
, eg, set var
to 10.2
?
The following will change the value:
import ctypes
lib = ctypes.cdll.LoadLibrary('x')
fun = lib.fun
fun.restype = ctypes.POINTER(ctypes.c_double)
v = fun()
v.contents.value = 2.0
v = fun()
Output (note for C function I used %p
instead of %zd
):
000007FEFAC14000 4.000000
000007FEFAC14000 2.000000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.