V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 3840 views
    This topic created in 2767 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
        1
    billchenchina  
       Oct 10, 2018 via Android
    解引用
    *a
    *b
    ule
        2
    ule  
    OP
       Oct 10, 2018
    @billchenchina 不太明白啊..
    ule
        3
    ule  
    OP
       Oct 10, 2018
    @billchenchina 我输出了下, 就只是一个类似地址的值 c_long(60654488), 这并不是真正的结果
    wwqgtxx
        4
    wwqgtxx  
       Oct 10, 2018
    a.value
    b.value
    ule
        6
    ule  
    OP
       Oct 10, 2018 via iPhone
    @wwqgtxx .value 也是数字地址
    传进去是一个 int 指针,c 函数对这个指针进行了处理,最终是一个字符串
    ule
        7
    ule  
    OP
       Oct 10, 2018 via iPhone
    @wwqgtxx 如果知道怎么把指针地址的值取出来就稳了
    reus
        8
    reus  
       Oct 10, 2018
    只能传指针的指针
    就像你想改变变量,就需要传变量的指针,那需要改变指针,就要传指针的指针
    xinali
        9
    xinali  
       Oct 10, 2018
    ule
        10
    ule  
    OP
       Oct 10, 2018
    @xinali c 改变了地址, python 不知道咋获取
    alixali
        11
    alixali  
       Oct 10, 2018
    这个是不是要*两次才能取到实际的赋值。
    ule
        12
    ule  
    OP
       Oct 10, 2018
    已解决,问题在 c 函数的传参上
    int* b,是一个指针地址, 不是 int 参数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   956 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 22:21 · PVG 06:21 · LAX 15:21 · JFK 18:21
    ♥ Do have faith in what you're doing.