title: Getting Array of struct from IntPtr
body: <p>I have some struct like this</p>
  
  <pre><code>struct MyStruct
  {
      public int field1;
      public int field2;
      public int field3;
  }
  </code></pre>
  
  <p>and I have pointer to array of this struct.
  So, I need to get array from this pointer.
  I'm tried to using Marshal.PtrToStructure, but i had memory reading error.
  This is my methode:</p>
  
  <pre><code>public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length)
  {
      var sizeInBytes = Marshal.SizeOf(typeof(TCnt));
      MyStruct[] output = new MyStruct[length];
  
      for (int i = 0; i &lt; length; i++)
      {
          IntPtr p = new IntPtr((pointerToStruct.ToInt32() + i * sizeInBytes));
  
          output[i] = (MyStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(MyStruct));
      }
  
      return output;
  }
  </code></pre>
  
  <p>So, what am i doing wrong ?</p>