推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
ule
V2EX  ›  Python

在调用 c 函数的时候,传参为指针类型, 调用后如何拿到指针的值呢?

  •  
  •   ule · Oct 10, 2018 · 4365 views
    This topic created in 2954 days ago, the information mentioned may be changed or developed.
    c:
    void test(int* a, int* b)

    python:
    a = c_int(0)
    b = c_int(0)
    test(byref(a), byref(b))

    调用后 a,b 都已经被赋值了;

    a,b 的类型是 <class 'ctypes.c_long'>

    byref 处理后的类型是 <class 'CArgObject'>

    如何才能从指针中拿到真正的值呢?
    12 replies  •  2018-10-10 10:42:09 +08:00
    billchenchina
    billchenchina
    Oct 10, 2018 via Android
    1
    解引用
    *a
    *b
    ule
    ule
    OP
    Oct 10, 2018
    2
    @billchenchina 不太明白啊..
    ule
    ule
    OP
    Oct 10, 2018
    3
    @billchenchina 我输出了下, 就只是一个类似地址的值 c_long(60654488), 这并不是真正的结果
    wwqgtxx
    wwqgtxx
    Oct 10, 2018
    4
    a.value
    b.value
    ule
    ule
    OP
    Oct 10, 2018 via iPhone
    6
    @wwqgtxx .value 也是数字地址
    传进去是一个 int 指针,c 函数对这个指针进行了处理,最终是一个字符串
    ule
    ule
    OP
    Oct 10, 2018 via iPhone
    7
    @wwqgtxx 如果知道怎么把指针地址的值取出来就稳了
    reus
    reus
    Oct 10, 2018
    8
    只能传指针的指针
    就像你想改变变量,就需要传变量的指针,那需要改变指针,就要传指针的指针
    ule
    ule
    OP
    Oct 10, 2018
    10
    @xinali c 改变了地址, python 不知道咋获取
    alixali
    alixali
    Oct 10, 2018
    11
    这个是不是要*两次才能取到实际的赋值。
    ule
    ule
    OP
    Oct 10, 2018
    12
    已解决,问题在 c 函数的传参上
    int* b,是一个指针地址, 不是 int 参数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1021 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 22:37 · PVG 06:37 · LAX 14:37 · JFK 17:37
    ♥ Do have faith in what you're doing.