【发布时间】:2021-03-17 07:06:21
【问题描述】:
我正在尝试在Cython 中使用void 指针,但我不知道如何使用它们。
在C 我可以这样做:
#include <stdio.h>
int n = 5;
void* ptr = &n;
printf("%d", *(int*)ptr);
并将其转换为 Cython 我尝试使用两者:
from libc.stdio cimport printf
cdef int n = 5
cdef void* ptr = &n
printf("%d", (<int*>ptr))
和
printf("%d", *(<int*>ptr))
有什么建议吗?
编辑
我能够通过使用 John Bollingers 的答案和使用以下方法来解决它:
from cython.operator cimport dereference
from libc.stdio cimport printf
cdef int n = 5
cdef void* ptr = &n
printf("%d", dereference(<int*>ptr))
【问题讨论】:
-
我认为this answer 可能会满足您的要求。