Note that there are some explanatory texts on larger screens.

plurals
  1. POResize ctypes array
    primarykey
    data
    text
    <p>I'd like to resize a ctypes array. As you can see, ctypes.resize doesn't work like it could. I can write a function to resize an array, but I wanted to know some other solutions to this. Maybe I'm missing some ctypes trick or maybe I simply used resize wrong. The name c_long_Array_0 seems to tell me this may not work with resize.</p> <pre><code>&gt;&gt;&gt; from ctypes import * &gt;&gt;&gt; c_int * 0 &lt;class '__main__.c_long_Array_0'&gt; &gt;&gt;&gt; intType = c_int * 0 &gt;&gt;&gt; foo = intType() &gt;&gt;&gt; foo &lt;__main__.c_long_Array_0 object at 0xb7ed9e84&gt; &gt;&gt;&gt; foo[0] Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; IndexError: invalid index &gt;&gt;&gt; resize(foo, sizeof(c_int * 1)) &gt;&gt;&gt; foo[0] Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; IndexError: invalid index &gt;&gt;&gt; foo &lt;__main__.c_long_Array_0 object at 0xb7ed9e84&gt; &gt;&gt;&gt; sizeof(c_int * 0) 0 &gt;&gt;&gt; sizeof(c_int * 1) 4 </code></pre> <p>Edit: Maybe go with something like:</p> <pre><code>&gt;&gt;&gt; ctypes_resize = resize &gt;&gt;&gt; def resize(arr, type): ... tmp = type() ... for i in range(len(arr)): ... tmp[i] = arr[i] ... return tmp ... ... &gt;&gt;&gt; listType = c_int * 0 &gt;&gt;&gt; list = listType() &gt;&gt;&gt; list = resize(list, c_int * 1) &gt;&gt;&gt; list[0] 0 &gt;&gt;&gt; </code></pre> <p>But that's ugly passing the type instead of the size. It works for its purpose and that's it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload