Python interfacing with C library – How to have a null c pointer

Question:

I have the following old c code.

const char *c[3];
c[0] = "ABC";
c[1] = "EFG";
c[2] = 0;
c_function(c);

Now, I need to use Python to call old c function. I have the following code.

c_names = (c_char_p * 3)()
c_names[0] = "ABC";
c_names[1] = "EFG";
// c[2] = 0??
libc = CDLL("c_library.dll")
libc.c_function(c_names)

May I know what is the Python equivalent for c[2] = 0;?

Asked By: Cheok Yan Cheng

||

Answers:

pointer = None

http://docs.python.org/library/ctypes.html

check 15.16.1.3. Calling functions

Answered By: Anycorn

None and 0 both work:

>>> import ctypes
>>> x=ctypes.c_char_p(0)
>>> x
c_char_p(None)
>>> x=ctypes.c_char_p(None)
>>> x
c_char_p(None)
Answered By: Mark Tolonen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.