最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to correctly initialize ctypes char***? - Stack Overflow

programmeradmin7浏览0评论

I'm using ctype to call C code from Python.

The C function I need to call takes a char***, and so it's bind as using a ctypes.POINTER(ctypes.POINTER(ctypes.c_char)).

I don't understand how I should safely create and initialize such objects, because if I create one and immediatly try to iterate it, the Python program crashs:

import ctypes
names = ctypes.POINTER(ctypes.POINTER(ctypes.c_char))()
if names != None:
    for name in names:
        pass

Error is:

Traceback (most recent call last):
  File "example_sdetests_lib_bind_python_string_array.py", line 6, in <module>
    for name in names:
ValueError: NULL pointer access

How can I safely check a ctypes.POINTER(ctypes.POINTER(ctypes.c_char)) is NULL or not?

I'm using ctype to call C code from Python.

The C function I need to call takes a char***, and so it's bind as using a ctypes.POINTER(ctypes.POINTER(ctypes.c_char)).

I don't understand how I should safely create and initialize such objects, because if I create one and immediatly try to iterate it, the Python program crashs:

import ctypes
names = ctypes.POINTER(ctypes.POINTER(ctypes.c_char))()
if names != None:
    for name in names:
        pass

Error is:

Traceback (most recent call last):
  File "example_sdetests_lib_bind_python_string_array.py", line 6, in <module>
    for name in names:
ValueError: NULL pointer access

How can I safely check a ctypes.POINTER(ctypes.POINTER(ctypes.c_char)) is NULL or not?

Share Improve this question asked Jan 17 at 15:33 jpo38jpo38 21.6k12 gold badges81 silver badges180 bronze badges 6
  • char*** is a 3-star pointer. Don't you need ctypes.POINTER(ctypes.POINTER(ctypes.POINTER(ctypes.c_char))) ? (i.e. another level) – wohlstad Commented Jan 17 at 17:39
  • @wohlstad: I dont think so, I'm able to call the function with that, only I get errors when the parameter is not set by the function, si it remains uninitialized. If set by the function it works fine sonI suppose the variable initialization is Ok. – jpo38 Commented Jan 17 at 19:13
  • Could be. It was just a guess: because char*** that you mentioned is a pointer-to-pointer-to-pointer-to-char (3 levels), and not just a a pointer-to-pointer-to-char (2 levels). – wohlstad Commented Jan 17 at 19:18
  • @wohlstad: I have the same behaviour if I call names = ctypes.POINTER(ctypes.POINTER(ctypes.POINTER(ctypes.c_char)))(), then for name in names: produces the same crash. – jpo38 Commented Jan 17 at 21:38
  • 1 Just bool(names) or if names: ... to check for a null pointer. Much like C pointers. How to initialise is a more complicated question. Depends on what you want to initialise the value as, and who is meant to own and manage the memory allocation (C or Python) – Dunes Commented Jan 17 at 23:16
 |  Show 1 more comment

1 Answer 1

Reset to default 1

FYI: char*** does need three ctypes.POINTER.

To test for null, test like any other "falsey" object:

import ctypes as ct

# define char*** type
PPPCHAR = ct.POINTER(ct.POINTER(ct.POINTER(ct.c_char)))

# instantiate (default null)
names = PPPCHAR()
if not names:
    print('NULL')
    
# Simple initialization
c = ct.c_char(b'a')
# pointer(obj) creates a ctypes pointer instance to a ctypes object.
pc = ct.pointer(c)
ppc = ct.pointer(pc)
pppc = ct.pointer(ppc)

if pppc:
    print('non-NULL')

print(pppc.contents.contents.contents)

Output:

NULL
non-NULL
c_char(b'a')
发布评论

评论列表(0)

  1. 暂无评论