Multi-dimensional char array (array of strings) in python ctypes

后端 未结 1 1822
野性不改
野性不改 2021-01-13 02:48

I\'m trying to pass an array of character arrays to a C function using ctypes.

void cfunction(char ** strings)
{
 strings[1] = \"bad\"; //works not what I ne         


        
1条回答
  •  一整个雨季
    2021-01-13 03:18

    Use something like

    input = ((c_char * 4) * 2)()
    input[0].value = "str"
    input[0][0] == "s"
    input[0][1] == "t" # and so on...
    

    Simple usage:

    >>> a =((c_char * 4) * 2)()
    >>> a
    <__main__.c_char_Array_4_Array_2 object at 0x9348d1c>
    >>> a[0]
    <__main__.c_char_Array_4 object at 0x9348c8c>
    >>> a[0].raw
    '\x00\x00\x00\x00'
    >>> a[0].value
    ''
    >>> a[0].value = "str"
    >>> a[0]
    <__main__.c_char_Array_4 object at 0x9348c8c>
    >>> a[0].value
    'str'
    >>> a[0].raw
    'str\x00'
    >>> a[1].value
    ''
    >>> a[0][0]
    's'
    >>> a[0][0] = 'x'
    >>> a[0].value
    'xtr'
    

    0 讨论(0)
提交回复
热议问题