【问题标题】:How to use void pointers in Cython如何在 Cython 中使用 void 指针
【发布时间】: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))

【问题讨论】:

标签: python c cython


【解决方案1】:

与 C 不同,Cython does not have a unary * operator。使用数组表示法取消引用指针:

from libc.stdio cimport printf
cdef int n = 5
cdef void *ptr = &n
printf("%d", (<int*>ptr)[0])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2014-05-30
    • 2016-06-30
    • 1970-01-01
    • 2012-02-20
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多