title: SWIG C++: Access pointer inside struct
body: <p>I have a struct as shown below defined in my header file (myheader.h)</p>
  
  <pre><code>typedef struct
  {
      MyMemberStruct* member;
      int32_t n_members;
  } MyMember;
  
  MyMember* MyMemberCreate(...)
  </code></pre>
  
  <p>The SWIG interface file </p>
  
  <pre><code>%module members
  
  %{
  #define SWIG_FILE_WITH_INIT
  #include "myheader.h"
  %}
  
  %include "myheader.h"
  </code></pre>
  
  <p>In the function <code>MyMemberCreate()</code> a list of <code>n_members</code>members are created and stored in <code>member</code>.</p>
  
  <p>In Python I would to access these members like:</p>
  
  <pre><code>pyMember = members.member[0]
  </code></pre>
  
  <p>But this gives the following error:</p>
  
  <blockquote>
    <p>'MyMemberStruct' object does not support indexing</p>
  </blockquote>
  
  <p>I have learnt that I can add the following to the interface file</p>
  
  <pre><code>%extend MyMember {
      MyMemberStruct __getitem__(int32_t index) {
          if (index &lt; $self-&gt;n_members) {
              return $self-&gt;member[index];
          }
      }
  }
  </code></pre>
  
  <p>Then from Python I can access the members as</p>
  
  <pre><code>pyMember = members[0]
  </code></pre>
  
  <p>which should me give me the first index in <code>MyMemberStruct</code></p>
  
  <p>Can anyone point me in the right direction of how I can achieve the first approach, i.e. <code>pyMember = members.member[0]</code> instead?</p>