I am working with a C/C++ library that has a Python API and on the Python side I encountered this piece of code when passing a Python list to C
from ctypes import c_int, c_size_t
def cvecint(o):
return (c_int * len(o))(*o), c_size_t(len(o))
which when called with an empty list, returns what appears to be a 0-length array
>>> cvecint([])
(<__main__.c_int_Array_0 object at 0x7f53470fa940>, c_ulong(0))
I thought 0-length arrays in C were not part of the standard, see What happens if I define a 0-size array in C/C++?. Same goes for C++, with both C arrays and std::vector
s.
Am I missing something? This code is not standard compliant, does it work purely based on some compiler extension for 0-length arrays? And if so, is there a better way of passing an empty Python list to C (and possibly C++)?